So uhh...anyone know any C programming?

New Member
💻 Oldtimer
Joined
Dec 3, 2002
Messages
2,490
Best answers
0
Here's the scenario: my keyboardist is trying to make a program that is essentially a very simple calculator. She's using an OSX mac, and using something called emacs, to code this very simple program in C. It's not working, she's at a loss, and if she doesn't finish it ASAP she won't be able to graduate.

Basically it's supposed to calculate, store and recall a value, I believe. Her exact words of her problem:
its supposed to store numbers, and when you try to print out the memory theres a bunch of random like 30 digit numbers in there that i didnt store and if i try to clear the memory it doesnt work
If anyone could point me and her toward some kind of tutorial of some sort, that would be very helpful!

Also, if anyone is interested or can fix it, here is her code:. She's really desperate, for what it's worth. I know you guys are pretty sharp, hopefully someone will be able to help us out!

#include <stdio.h>
//Declare the global size of the calculator memory.

const int gMemSize=20;

void clearMem(int value1, int value2, float n[])
{
int x;
if (value2>0 && value2<gmemsize) {="">
for (x>value1; x<=value2; ++x)
n[x]=0;

}
else
printf("out of range of defined memory\n");

}



void printResult(float result) //eliminates use of
printf statements
{
printf("%.2f\n", result);
}



float power(float value1, float value2)

{
int i;
int v;

if(value2 > 0)
{
v=value1;
for (i=1; i<value2; i++)="">
value1=value1*v;
printf("%.2f\n", value1);
}
else
if(value2==0)
{
value1=1;
printf("%.2f\n", value1);
}
else
if(value2< 0 && value1 != 0)
{
value2=value2 * -1;
v=value1;
for(i=0; i<value2; i++)="">
value1=value1/v;
printf("%.2f\n", value1);
}
else
if (value2<0 && value1==0)
{
printf("cannot divide by zero!!\n");
}
}

int main(void)
{
float value1;
float value2;
float n[gMemSize];
char operator;
int i;
int v;


\

clearMem(0, (gMemSize - 1), n);//Initialize the
calculator's memory to 0.0

printf ("Type in a math expression. \n");
while (1)
{
printf (">");
scanf ("%f %c %f", &value1, &operator, &value2);

switch (operator)
{
case 'A':
value2=n[(int)value2];
case '+':
printResult (value1+value2);
break;

case 'S':
value2=n[(int)value2];
case '-':
printResult (value1-value2);
break;

case 'M':
value2=n[(int)value2];
case '*':
case 'x':
printResult (value1*value2);
break;

case 'D':
value2=n[(int)value2];
case '/':
if(value2==0)
printf("cannot divide by zero!!\n");
else
printResult( value1/value2);
break;

case 'P':
value2=n[(int)value2];
case '^':
printResult(power(value1, value2));
break;

case 's': //SAVING MEMORY LOCATIONS

n[(int)value2] = value1;
printf("stored memory location #%i.\n",
(int)value2);
break;
case 'p': //RECALLING LOCATIONS

for(i=value1; i<=value2; i++)
printf("value #%i -- %.2f\n", i, n);
break;

case 'c': //clears memory
clearMem(value1, value2, n);
break;
default:
printf("operator unknown!!\n");
break;
}
}
return 0;
}
</value2;></value2;></gmemsize)></stdio.h>
 
New Member
💻 Oldtimer
Joined
Nov 29, 2004
Messages
1,626
Best answers
0
Over my head. I gave up reading through code the minute I finished my CompE final.
 
New Member
💻 Oldtimer
Joined
Dec 3, 2002
Messages
2,490
Best answers
0
Damn. I bet it's something as simple as a syntax error somewhere. I mean, it works, it is just spitting out bad numbers. So it must just have a number or something in the wrong place.
 

jp

New Member
💻 Oldtimer
Joined
Aug 23, 2003
Messages
1,561
Best answers
0
I am by no means someone who can program in C, or in PHP for that matter but I have tried once,

Maybe it helps to check if everything has ended with ; that should end with it.

From what I know, every command/syntax should end with a ;

and the
Code:
float power(float value1, float value2)
does not end with one, as for a few others.

:3 but i'm not a coder really... but it never hurts to try.
 
New Member
Joined
Oct 20, 2004
Messages
397
Best answers
0
i'm just gonna throw a bunch of stuff out that COULD be problems, its often hard to tell with code(i also don't have a compiler handy :( )

in clearmem the if statement is missing a right parenthesis also the X is not initialized(set to a first value). There's also a missing {. i'm thinking hte X not being initialized could cause problems(unless the compiler she's using initializes it to 0 for you). I'll edit more as i find it.

edit: in clearmem the X > value1 in the for loop is odd to me might wanna check it.

edit 2: it seems like a lot of the if/for statements are i ncomplete, did you copy the code correctly?

edit 3: float power(float value1, float value2) doesn't need to end in a ; it's the start of a function(you only need to do that when youa re prototyping)

edit 4:
case 'A':
value2=n[(int)value2];
case '+':
printResult (value1+value2);
break;
for this code if case A is done + is also going to be done regardless if a + was ever entered because theres no break statement at the end of the A case

edit 5: scratch edit 4, i understand why she's doing that, but i don't understand why case A has code that case + doesn't

edit 6: value2=n[(int)value2] is going to cause problems for numbers bigger than the array size(writing/reading to bad parts of an array can often give odd results like she seems to be getting.

edit 7: i also suggest automating the clearmem function, as your array is static(it doesn't change size) you shoulnd' tneed to give it any parameters, as you(the programmer) should be able to hard code in the values.

edit 8: it looks to me like the problem is in the array, if value2 is ever greater than the array size(gMemSize) bad thins are going to happen if any of the letters are input. i would suggest making a seperate array that you edit every time one of the case statements goes through, and then when you save set the elements of the second array equal to the first array, and then continue editing the first array.


Anyway those are the big things i can see, i hope they helped(and i hope they aren't too late). good luck.
 
New Member
Joined
Nov 24, 2001
Messages
692
Best answers
0
Can't really tell, since some code seems to be fallen off, no way it will compile like that...

But clearMem leaves x uninitialized (meaning that it could be whatever value was in the memory earlier), so that'd explain odd memory clearage. Should be
for (x=value1; x<=value2; ++x) rather than
for (x>value1; x<=value2; ++x)

also, that's a rather odd way to calculate a power, why not just use the pow function?
 
New Member
Joined
Oct 20, 2004
Messages
397
Best answers
0
yah i wasn't sure on her power function either, but since the for statements are chopped off its kinda hard to tell.

oh also your power function doesn't return anything. has she actually compiled this code? it doesn't seem like it should compile(power not returning anything should DEFINENTLY be a compiler error).

edit: one more big thing i just thought of, when she sets value1 and value2 = to things in the power function it doesn't actually change value1 and value2 in her main function. When you pass non arrays to functions it actually creates a copy it. So when she does power(value1, value2), the power function makes its own value1 and value2 to edit. If you want to edit the initial value1 and value2 the parameter has to be a point( *float value2) and the function call has to be an address (&value2).

i'll give a bit of sample code for how i would do add.

case 'A':
case'+':
printResult (value1+value2);

if(i < gMemSize - 1)
n[i++] = value1 + value2
else
printf("Memory is full");
break;

this way it saves the added value into an array which you can later copy into another array during the save process. this way also checks if youa re about to go out of bounds.

edit again:

also #define MEMSIZE 20 is better than making a global variable(although that's an effeciency and format thing not a coding problem).

edit again again: sorry for my constanst switching of you and her
 
New Member
Joined
May 1, 2006
Messages
665
Best answers
0
Whew.... She works with such messy code? I mean the identation bit.... I can hardly work with such code myself.
I suppose I fixed most of it up. The only problem now is the line which I'll specify. Ask her what she needed to be put there.

Code:
#include < stdio.h > //Had to put spaces or this forum removes it thinking its HTML...
#include < conio.h >
//Declare the global size of the calculator memory. 

const int gMemSize=20;

void clearMem(int value1, int value2, float n[])
{
	int x;
	if (value2>0 && value2  //[B]Whats this do? Its incomplete[/B]
	{
		for (x>value1; x<=value2; ++x)
		n[x]=0;
	}
	else
		printf("out of range of defined memory\n");
}



void printResult(float result) //eliminates use of printf statements 
{
	printf("%.2f\n", result);
}



float power(float value1, float value2)
{

	int i, v;

	if(value2 > 0)
	{
		v=value1;
		for (i=2; i<=value2; i ++)
			value1=value1*v;
		printf("%.2f\n", value1);
	}
	else if(value2==0)
	{
		value1=1;
		printf("%.2f\n", value1);
	}
	else if(value2< 0 && value1 != 0)
	{
		value2=value2 * -1;
		v=value1;
		for(i=2; i<=value2; i++)
			value1=value1/v;
		printf("%.2f\n", value1);
	}
	else if (value2<0 && value1==0)
	{
		printf("cannot divide by zero!!\n");
	}

	return value1;
}

int main(void)
{
	float value1;
	float value2;
	float n[gMemSize];
	char operator;
	int i, v, loopConst=1;
	clrscr(); //clear screen
	clearMem(0, (gMemSize - 1), n)) //Initialize the calculator's memory to 0.0
	printf ("Type in a math expression. Hit Q to exit\n");
	while (loopConst)
	{
		printf (">");
		scanf ("%f %c %f", &value1, &operator, &value2);

		switch (operator)
		{
			case 'A': value2=n[(int)value2];
			case '+': printResult (value1+value2);
				  break;

			case 'S': value2=n[(int)value2];
			case '-': printResult (value1-value2);
			break;
			case 'M': value2=n[(int)value2];
			case '*':
			case 'x': printResult (value1*value2);
	   			  break;
			case 'D': value2=n[(int)value2];
			case '/': if(value2==0) 
					printf("cannot divide by zero!!\n");
				  else
					printResult( value1/value2);
				  break;

			case 'P': value2=n[(int)value2];
			case '^': printResult(power(value1, value2));
				  break;
			case 's': //SAVING MEMORY LOCATIONS 
				  n[(int)value2] = value1;
				  printf("stored memory location #%i.\n", (int)value2);
				  break;
			case 'p': //RECALLING LOCATIONS 
				  for(i=value1; i<=value2; i++)
				 	 printf("value #%i -- %.2f\n", i, n[i]);
				  break;
			case 'c': //clears memory
		  		  clearMem(value1, value2, n);
				  break;
			case 'q' : //To quit or not to quit.
			case 'Q' : loopConst=0;
				   break;
			default : printf("operator unknown!!\n");
			 	 break; //Uneccesary!!!!
		}
	}
	return 0;
}

I added a bit. Fixed her power function up. And I'd listen to harSens, though. Why'd she use the definition of what is actually provided in header files? She could have used the power function.

What suits oneself, I suppose.
 
Senior Member
💻 Oldtimer
Joined
Oct 21, 2003
Messages
2,706
Best answers
0
I'm sure this awesome programmer I know named Kurt could help...but he is kinda.....kabanz0red. Does look messy though, good luck with that.
 
New Member
💻 Oldtimer
Joined
Dec 3, 2002
Messages
2,490
Best answers
0
She's a musician, the ideas of computing and programming are completely alien to her. Add to that the fact she missed some key classes during her semester and well, you end up with a mess.

Either way, she and I greatly appreciate all the help.
 
Active Member
✔️ HL Verified
💻 Oldtimer
Joined
Nov 6, 2004
Messages
3,055
Best answers
0
Location
Round Rock, TX
Can't really tell, since some code seems to be fallen off, no way it will compile like that...

But clearMem leaves x uninitialized (meaning that it could be whatever value was in the memory earlier), so that'd explain odd memory clearage. Should be
for (x=value1; x<=value2; ++x) rather than
for (x>value1; x<=value2; ++x)

also, that's a rather odd way to calculate a power, why not just use the pow function?
Whoa, I thought you weren't a C/C++ help desk? When did this change?
 
New Member
Joined
Nov 24, 2001
Messages
692
Best answers
0
I don't feel like helping people code one on one ever. Especially if their problems are 'my code is broken/won't compile'. Conceptually interesting questions I might look over though. This thread just caught my attention and I quickly saw some things that were wrong/odd.

Anyhooter, ontopic:
I'd definatly look at the problem I mentioned above and the power function not returning anything (I'd just replace it with pow, from math.h). The other stuff COULD be fine, depending on what she has in mind. If it still doesn't work then, the easiest way to check on it is going through it with the debugger and check on the values of the memory. Or throw in tons of printf's.
 

Users who are viewing this thread

Top Bottom