Need some help working around loops for a C program.

Cat1yst

[H]ard|Gawd
Joined
Jan 1, 2007
Messages
1,616
Ok so i already have the basis for the program down, (linked below), basically however the catch 22 is that for the first part of the assignment, for whatever reason my prof wanted us to figure out a way to repeat my overall functions down below seven times to create a final printout of 7 separate inputs, and then once we had that down go ahead and use loops.

Is there any way I can repeat my function codes without having to copy and pasta and manually edit in the pointers and variables and such? Im also assuming that if this is the only way without loops that I would have to also edit everything in my functions and thus cannot simply just state multi variables in my main.
Code:
 #include <stdio.h>


void Input (float *x)
{	//accepts user input
	printf("Please enter in the current salaries of the 7 employees \n" );
	scanf("%f", x);
}

float cond(float x)
{ //establishes the rate of raise to be used
	float r;
	if (x >= 0 && x< 30000) r=.07;
	if (x >= 30000 && x< 40000) r=.055;
	else r = .04;
	return r;
}

void calc (float x, float r, float *rs, float *rp, float *s )
{ //take what i got from cond and then multiply it to find raise rate, raise percent in terms of percent, and new salary
	*rs=x*r;
	*rp=r*100;
	*s=x*r +x;
}

void print (float x, float rp, float rs, float s)
{ //prints the shit out.
	printf(" Salary    Rate    Raise   New Salary \n");
	printf(" %f, %f, %f, %f", x, rp, rs, s);
}
	
void main ()
{ 
	float x, r, rp, rs, s;
	Input(&x);
r = cond(x);
calc( x, r, &rp, &rs, &s);
print(x, rp, rs, s);
getchar();
}
Thanks for helping!

PS. is there a way to arrange by grid my printout thats relatively easy for a noob? ive looked it up once and it looked quite confusing.

PSS. If it helps i have a hand written function chart and can post that up.
 
I don't really understand. So you know the basics of pointers, functions, but did not learn loops yet? It seems a bit of an opposite way to teach programming. When I took my first programming course, Java, loops were one of the first things covered.

I am assuming the following would work, if this was C++. I am not a C person, but from what I remember the declaration of the loop variable should be before the actual loop.
Code:
void main ()
{
  for(int i=0; i<7; i++)
  { 
    float x, r, rp, rs, s;
    Input(&x);
    r = cond(x);
    calc( x, r, &rp, &rs, &s);
    print(x, rp, rs, s);
    getchar();
  }
}

Perhaps I don't understand your question fully. Hopefully that helps though.
 
After reading it again, maybe I understand it now. So you need a way to repeat the program multiple times, without the use of loops?

The most obvious way would be to launch the program multiple times and gather the output. If that's out of question, you could probably fork the program X number of times, but then the standard input needs to be hardcoded instead, which might defeat the purpose.

Probably a better way would be to use recursion instead, which I think would solve your problem.
Code:
void main(int argv)
{
  if(argv == 0)
    return 0;
  float x, r, rp, rs, s;
  Input(&x);
  r = cond(x);
  calc( x, r, &rp, &rs, &s);
  print(x, rp, rs, s);
  getchar();
  main(--argv);
  return 0;
}

Perhaps something like this.

One problem that I see with this is with the header of main. If you call the program from the console passing it a "1", the "1", I think, will be treated as an ascii value and casted to an int, becoming the value of 49 instead.

Try it and see what happens. Maybe it won't even compile. I dunno, just want to help. ;)
 
would using recursion be an option?
print our your result then call the function again.
 
Thanks all for the reply, i actually have a test on loops coming up today :eek:

Basically he assigned this while introducing us to loops, for whatever reason he wanted us to employ local variables and functions, along with pointers and references for this assignment. (for a once a week, night class i really cant blame the prof), once this assignment was due he then started introducing loops ><

I guess other than using "alternatives" to a loop, it doesnt seem that i have any other choice but to manually dictate the loop....

One or two of the more experienced comp sci majors did use recursion, and technically he didnt say we couldnt use recursions or arrays......

EDIT: ehhhh. fuck it, might as well teach myself recursions :p
 
He probably didn't want any crazy programming techniques; just wanted you to do it out manually. Then when you learn loops, showing you the power of loops verses writing the same thing out multiple times.
 
So i went ahead and did do it out manually, and then i got my loop to work. The only issue was that he didnt teach us arrays until the session before the homework was due, and thus i was left simply looping everything as i could not save all seven values to an array and then recall and printf them all at the same time.


Thanks all!
 
Well you could still re-use the variables and just print them out before you go to the next loop iteration.

Just move the getchar() outside the loop... Unless you need the values around after you print them... I didn't look too close.
 
Back
Top