Post your C/C++ Programs Here

Status
Not open for further replies.

Glen Apayart

Right off the assembly line
Re: Post ur C/C++ Programs Here

@Sykora

Nevermind ^^. im talking about the techniques of a function which is the "inline function" and the other one was the "argument by reference" that are considered to be defined. Anyway, i understood it now.

Sorry for my bad english.
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

Cool.. seems like this thread helps people and students who are desperate to learn, solve, seeking help... So guys i think this thread should go sticky :D

I WILL POST A POLL to make this stick or not.. please vote :)
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

I would prefer a separate programming section (which I think has been raised a number of times).
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

QwertyManiac said:
I don't want this as sticky, cause no one would look at it then, as is the case with most stickies. I'd rather vote for a new Programming section :)

Same as Sykora, and quoting my old post from the same thread. But of course, no one listens ..
 

Ron

||uLtiMaTE WinNER||
Re: Post ur C/C++ Programs Here

this thread should go STICKY.........
and all the running c++ threads should be merged.....
 

Nav11aug

In the zone
Re: Post ur C/C++ Programs Here

exactly..we dnt want a huge mess here.A new programming sectiona nd no merges whtsoever
 
OP
Gigacore

Gigacore

Dreamweaver
Re: Post ur C/C++ Programs Here

Yeah,, after making the section let them move this thread to that forum and make it sticky :D
 

ilugd

Beware of the innocent
Re: Post ur C/C++ Programs Here

can we get back to the topic? it was getting interesting. :)
 

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

^^ Yep... it was. I had gone to a competition of C programming today. Me and my partner, we ranked third in debugging contest and maybe 5th in coding contest (dunno for sure, but we were one of the 5 that finished the coding in time :-D)

Read more here on my blog
 

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

^^ Getting back to the contest mentioned above, we had to do this program as first assigment :-

Code:
/* This program calculates the value of a mathematical function and gives */
/* the output depending on it. The function is defined as follows :-      */
/* f(1)    = 1                                                            */
/* f(2n)   = n                                                            */
/* f(2n+1) = f(n) + f (n+1)                                               */
/* The inputs should be taken till a negative value is inputed.           */
/* The maximum number of inputs is considered to be 100.                  */

#include<iostream>

#define _maxInputs 100

using namespace std;

float Function(int);

main()
{
	int* inNumberArray = new int[_maxInputs]; 
	float* outAnswerArray = new float[_maxInputs];
	int cnt, counter;
	
	cout << endl;
	cout << "Please enter the variable values. Enter negative value to exit";
	cout << endl;
	
	for(counter = 0; counter < _maxInputs; counter++)
	{
		cin >> inNumberArray[counter];
		
		if(inNumberArray[counter] < 0)
			break;
		
		outAnswerArray[counter] = Function(inNumberArray[counter]);
		
	}
	
	cnt = counter;
	
	for(counter = 0; counter < cnt; counter++)
	{
		cout << "F(" << inNumberArray[counter] << ") = " << outAnswerArray[counter]
			 << "." << endl;
		
	}
	
	delete[] inNumberArray;
	delete[] outAnswerArray;
	
	return 0;
	
}

float Function (int inNumber)
{
	if(inNumber == 1)
		return 1.0;
	
	else if(inNumber % 2 == 0)
		return (inNumber/2.0);
	
	else if(inNumber % 2 == 1)
		return (Function((inNumber-1)/2) + Function((inNumber+1)/2));
	
}

The output is :-

Code:
EXECUTING:
/home/aditya/Mathematical-Function-1 
----------------------------------------------

Please enter the variable values. Enter negative value to exit
1 2 3 4 5 6 7 8 9 10 11 12 -1 
F(1) = 1.
F(2) = 1.
F(3) = 2.
F(4) = 2.
F(5) = 3.
F(6) = 3.
F(7) = 4.
F(8) = 4.
F(9) = 5.
F(10) = 5.
F(11) = 6.
F(12) = 6.

----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...

Is it any good? Any ideas for optimization? Anything?

Aditya
 

Sykora

I see right through you.
Re: Post ur C/C++ Programs Here

If you want to be really picky, the recursive call of your function can be modified as :

Code:
return !(n - 1) % 4 ? (n - 1)/4 + Function((n + 1)/2) : ((n + 1)/4 + Function((n - 1)/2);

All it will do is reduce the overhead of an extra function call. In C++, it won't make too much of a difference unless you are calling it a million times or so, but in a different language, it can cut down the running time dramatically. Also, for a language like python which has a dictionary data structure, you can use caching.
 

quan chi

mortal kombat
Re: Post ur C/C++ Programs Here

guys i have a problem.

well its a program where it firsts cubes a value and then squares it.

#include<iostream.h>
int cube(int x);
int square(int cubed);
void main()
{int x,cubed,squared;
cout<<"enter a value";
cin>>x;
cubed=cube(x);
squared=square(cubed);
cout<<"cubed"<<cubed;
cout<<squared"<<squared;
}
int cube(int x)
{return(x*x*x);
}
int square(int cubed)
{return(cubed*cubed);
}

well my problem is if i enter the value as 2 then the program works well.
if i enter the value as7 then it cubes 7 perfectly.
but gives its squared value as some 4 to 5 digit negative number.

then i changed the int(return type and declaration) to unsigned long then it worked perfectly.why is it so? why didn't it worked for int?
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

That is cause, in C/C++, signed int's range is just till 32768 in the positive range. Anything above that creates garbage. Use float for long digit calculations. Unsigned int is till 65536 and thus works for your solution.
 

aditya.shevade

Console Junkie
Re: Post ur C/C++ Programs Here

^^ Maybe the answer was exceeding the limit of integer data type.

And please format the code as is first post.
 

QwertyManiac

Commander in Chief
Re: Post ur C/C++ Programs Here

Wasn't that what I was trying to point out? :-\

Oh, guess you're saying it to him and not me :p
 

timemachine

Boom Bhadam Dhishkyao
Re: Post ur C/C++ Programs Here

Character Eater:

This TSR program hooks itself with timer interrupt and selects a random row and coloums position at each run and writes space at that position, the person using the computer feels that something is eating up the characters from the screen.

#include"dos.h"
#include<conio.h>
#include<stdlib.h>

//interrupt declarations
void interrupt (*prevtimer)();
void interrupt mytimer();
void writechar(char ch,int row,int col,int attr);

//a far pointer that will access video memory
char far* scr;
int a,b;

//our real program goes from here
void main()
{
scr=(char far*) 0xb8000000;
prevtimer=getvect( 8 );
setvect(8,mytimer);
keep(0,1000);
}

//timer function
void interrupt mytimer()
{
a=random(25);
b=random(80);
writechar(' ',a,b,0);
(*prevtimer)();
}

//function that writes picked up character
void writechar(char ch,int row,int col,int attr)
{
*(scr+row*160+col*2)=ch;
*(scr+row*160+col*2+1)=attr;
}
 
Status
Not open for further replies.
Top Bottom