Like Tree3Likes
  • 3 Post By grumpy

No clue how to do a loop.. please help

This is a discussion on No clue how to do a loop.. please help within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm at my wits ends, I'm trying to change a code that I wrote into loops and I ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    3

    No clue how to do a loop.. please help

    Hi everyone, I'm at my wits ends, I'm trying to change a code that I wrote into loops and I don't have any idea where to begin. It's for a class assignment and I can use a do, for, or while loop, but I can't figure out where to start changing everything over. Any help pointing me in the right direction would be appreciated. I'm very new at coding and I get the majority of it but loops are, well throwing me for a loop

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void load(float*s)
    {
        printf("Enter the salary");
        scanf("%f",&(*s));
    }
    float rate(float s)
    {
        if(s<30000)
            return 7.0;
        if(s>=30000&&s<=40000)
            return 5.5;
        if(s>40000)
            return 4.0;
    }
    void calc(float s,float rate,float*total,float*raise,float*totalr,float*news,float*totaln)
    {
        *raise=(s*rate)/(float)100;
        *total=*total+s;
        *news=*raise+s;
        *totalr=*totalr+*raise;
        *totaln=*totaln+*news;
    }
    void print(float s,float rate,float raise,float news)
    {
        printf("\t%0.2f\t%0.2f\t\t%0.2f\t\t%0.2f\n",s,rate,raise,news);
    }
    void main()
    {
        float s1,rate1,total=0.0,raise1,totalr=0.0,news1,totaln=0.0;
            load(&s1);
        float s2,rate2,raise2,news2;
            load(&s2);
        float s3,rate3,raise3,news3;
            load(&s3);
        float s4,rate4,raise4,news4;
            load(&s4);
        float s5,rate5,raise5,news5;
            load(&s5);
        float s6,rate6,raise6,news6;
            load(&s6);
        float s7,rate7,raise7,news7;
            load(&s7);
        rate1=rate(s1);
        calc(s1,rate1,&total,&raise1,&totalr,&news1,&totaln);
        printf("\tSalary\t\tRate%\t\tRaise\t\tNew Salary\n");
        print(s1, rate1, news1, news1);
        rate2=rate(s2);
        calc(s2,rate2,&total,&raise2,&totalr,&news2,&totaln);
        print(s2,rate2,raise2,news2);
        rate3=rate(s3);
        calc(s3,rate3,&total,&raise3,&totalr,&news3,&totaln);
        print(s3,rate3,raise3,news3);
        rate4=rate(s4);
        calc(s4,rate4,&total,&raise4,&totalr,&news4,&totaln);
        print(s4,rate4,raise4,news4);
        rate5=rate(s5);
        calc(s5,rate5,&total,&raise5,&totalr,&news5,&totaln);
        print(s5,rate5,raise5,news5);
        rate6=rate(s6);
        calc(s6,rate6,&total,&raise6,&totalr,&news6,&totaln);
        print(s6,rate6,raise6,news6);
        rate7=rate(s7);
        calc(s7,rate7,&total,&raise7,&totalr,&news7,&totaln);
        print(s7,rate7,raise7,news7);
        printf("Total\t%0.2f\t\t\t%0.2f\t%0.2f\n",total,totalr,totaln);
        system("PAUSE");
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    First thing is, you won't see how loops work if you keep s2, s3, s4, s5, s6 and s7 - because that's not swimming on dry land - you just won't get it to work.

    Using just s1, and the other 1 variables, start with a for loop:
    Code:
    for(i=0;i<7;i++)  { //the int variable i is your loop counter
       call load(), just like now
       call rate(), just like now
       call calc() same as now
       call print, just like now
    } //end of the for loop
    
    Now print up your totals, just like now.
    You will use your 1 variables, over and over, and never need 2, 3, 4, 5, 6, or 7 variables. Your code will be shortened substantially.

    You may want to run through the C tutorial linked at the top center of this page - lots of good stuff for you there.

    When you see yourself repeating code with just the most minor changes (as you have here), STOP! The people who created C were brilliant, and that's not what they wanted done by C programmers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Well ... you have a set of variables with names based on numbers (s1, s2, s3, rate1, rate2, rate3, etc) and you're copying and pasting code to use those variables (i.e. doing the same thing with s1 and rate1 as you're doing with s2 and rate2).

    That is a prime opportunity to use arrays, say named s and rate, and a loop to iterate over the elements and do the "thing" once in that loop. Just remember that array indices start at 0, not 1. (and that variables or arrays cannot have the same name as a function).

    I've described the concept. I will not provide code to illustrate. That is your job, not mine, since you're the one who as to learn the idea.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    3
    Hey guys, thanks for the help. I'm in an entry level C class and have only dabbled in Python, so to say I'm a noob at coding is an understatement, but I'd like to learn it and started with C and will definitely check out the tutorials. I appreciate the pointing towards the right direction, and don't want the answers, just eyes that actually know how to do it and can point out where I'm messing up. I had to add all of that code initially because we hadn't learned about loops yet and I'm not allowed to use arrays yet. I added the for loop and can actually get the "start without debugging" to work but my enter value comes out like this "Enter the salaryEnter the salary" 7x... I'm sure it's a small mistake but I can't see it. Any advice?

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void load(float*s)
    {
        int i;
        for(i=0;i<7;i++)  
        printf("Enter the salary");
        scanf("%f",&(*s));
    }
    float rate(float s)
    {
        if(s<30000)
            return 7.0;
        if(s>=30000&&s<=40000)
            return 5.5;
        if(s>40000)
            return 4.0;
    }
    void calc(float s,float rate,float*total,float*raise,float*totalr,float*news,float*totaln)
    {
        *raise=(s*rate)/(float)100;
        *total=*total+s;
        *news=*raise+s;
        *totalr=*totalr+*raise;
        *totaln=*totaln+*news;
    }
    void print(float s,float rate,float raise,float news)
    {
        printf("\t%0.2f\t%0.2f\t\t%0.2f\t\t%0.2f\n",s,rate,raise,news);
    }
    void main()
    {
        float s,rate,total=0.0,raise,totalr=0.0,news,totaln=0.0;
            load(&s);    
        calc(s,rate,&total,&raise,&totalr,&news,&totaln);
        printf("\tSalary\t\tRate%\t\tRaise\t\tNew Salary\n");
        print(s, rate, news, news);
        printf("Total\t%0.2f\t\t\t%0.2f\t%0.2f\n",total,totalr,totaln);
        system("PAUSE");
    }

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    The scanf() is not part of the for loop. If you want it to be, use curly braces.
    Code:
    for(i=0;i<7;i++) 
    {
        printf("Enter the salary");
        scanf("%f",s);
    }
    The printf() and scanf() lines are each a "statement". The curly braces groups them together into a single compound statement. Since the bit after the closing bracket of a for loop can only be a single statement ......

    I've also adjusted the usage of s in the scanf() statement. &(*s) and s are equivalent, if s is a pointer to float.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    3
    Thank you both for helping me, I messed around with it pretty much all night and finally figured it out. It's crazy how one tiny mistake can ruin a whole code. I posted my finished work below. I'm sure it's not the cleanest but, maybe one day.

    Code:
    #include<stdio.h>#include<stdlib.h>
    void load(float *s)
    {
        printf("Enter the salary");
        scanf("%f",(&*s));
    }
    
    
    float ckrate(float s)
    {
        if(s<30000)
            return 7.0;
        if(s>=30000&&s<=40000)
            return 5.5;
        if(s>40000)
            return 4.0;
    }
    void calc(float s,float rate,float*total,float*raise,float*totalr,float*news,float*totaln)
    {
        *raise=(s*rate)/(float)100;
        *total=*total+s;
        *news=*raise+s;
        *totalr=*totalr+*raise;
        *totaln=*totaln+*news;
    }
    void print(float s,float rate,float raise,float news)
    {           
        printf("\t%0.2f\t%0.2f\t\t%0.2f\t\t%0.2f\n",s,rate,raise,news);
    }
    void printTot(float total,float totalr,float totaln)
    {
        printf("--------------------------------------------------------------------------------");
        printf("\n\n");
        printf("Total\t%10.2f\t\t\t%10.2f\t\t%10.2f\n",total,totalr,totaln);
        printf("\n\n");
    }
    void main()
    {
        printf("Enter the Salary  \n\n");
        int i;
        float s,rate,total=0.0,raise,totalr=0.0,news,totaln=0.0;
        printf("\t    Salary\t     Rate%%\t     Raise\t\tNew Salary\n");
        for(i=0;i<=6;i++)
        {
            load(&(s));
            rate = ckrate(s);
            calc(s,rate,&total,&raise,&totalr,&news,&totaln);
            print(s,rate,raise,news);
            
        }
        printTot(total,totalr,totaln);
        system("PAUSE");
        }

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,261
    Quote Originally Posted by scottlbg View Post
    It's crazy how one tiny mistake can ruin a whole code.
    It's not actually. C - like most programming languages - allows doing lots of things, and allows changes to be made quickly, so it's no surprise that small things can change the meaning of code a lot. It is actually crazy that people keep thinking that they can be imprecise in their coding, and that the compiler will somehow do exactly what they want anyway.

    Even in the english language, a seemingly minor bit of punctuation can completely change the meaning of a sentence. "This is crazy, Jack" has a very different meaning from "This is crazy Jack". The removal of a single comma changes the meaning completely. And similar phenomena occur with most, if not all, natural languages. The more pedantic and ignorant the person (or entity) you're communicating with with, the more precise you need to be.

    Programming languages are interpreted by computer programs on computing machines. Computing machines tend to be pedantic ignoramuses so, if you want them to do what you expect, the expression of what you intend (i.e. your code) needs to be precise.

    Like it or not, there is no alternative for properly learning the language you are trying to use - or, at least, the parts of it you need to use. And then being very precise in how you express your solutions to problems.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to do this....
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 04-24-2007, 05:51 PM
  2. don't have any clue how to do this
    By newcstudent in forum C Programming
    Replies: 59
    Last Post: 10-10-2006, 12:05 AM
  3. No CLUE what to do...
    By stewade in forum C Programming
    Replies: 7
    Last Post: 05-03-2004, 12:02 AM
  4. Not a clue
    By cyberCLoWn in forum C++ Programming
    Replies: 11
    Last Post: 12-28-2003, 10:54 AM
  5. need a clue
    By srfrrgrrl in forum C++ Programming
    Replies: 10
    Last Post: 02-17-2003, 01:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21