Thread: HELP !! C language ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    HELP !! C language ...

    I wish to ask anyone for any advice on how this C program would flow.. and display out...

    I have no previous programming experience, and we just had 2 lecture sessions...

    Im really confused !! Please Help !!

    Thanks !!



    /*sums are computed */

    #include<stdio.h>
    intmain(void)
    {
    int cnt = 0;
    float sum = 0.0,x;
    printf("The sum of your numbers will be computed.\n")
    printf("Input some numbers: ");
    while(cnt <5)
    {
    scanf("%f",&x);
    sum = sum + x;
    cnt = cnt + 1;
    }
    printf("\n%s%12f\n\n sum:", sum);
    return0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    > I have no previous programming experience, and we just had 2 lecture sessions...
    Tell me what you think is happening in that program (if you don't know then guess) and I'll be happy to clarify any points you miss.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    22
    It should be...

    #include <stdio.h>
    #include <conio.h>

    int main( ) //space needed after function type
    {
    int cnt = 0;
    float sum,x;
    printf("The sum of your numbers will be computed.\n");
    printf("Input some numbers: ");

    while(cnt<5)
    {
    scanf("%f",&x);
    sum+=x;
    cnt++; //to increment by 1.. same as cnt=cnt+1 or cnt+=1
    }

    printf("\n\n\nSum: %12f", sum);
    getch();
    return 0; //space needed after the return keyword
    }

    You also forgot to add a semi-colon after one of the printf statements.. Remember that a semi-colon tells the compiler when an instruction ends and a new will begin. Program starts inside main and declares 3 variables. One of type int for integer numbers and 2 for type float for floating point numbers. Sends output to the screen using print formatted then gets inside a while loop. Each time a number is added it's also added to variable sum, then increments cnt by one. You can also say cnt=cnt+1 or cnt+=1 if you like... but cnt++ is easier to use and more common in programming.. After it loops 5 times it will printf out to the screen one last time displaying what variable sum holds. Then getch(); function waits until you press any key to close program. Note that getch(); is used with the conio.h header file and this is not a standard so your compiler might not be able to use this. Plus, if you are running this program from the prompt then you dont need to use getch();

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    I Need more than HELP !!

    could anyone explain the flow and output of this source code?
    without any additional variables and functions.... only the thing written here...


    /*sums are computed */

    #include<stdio.h>
    int main(void)
    {
    int cnt = 0;
    float sum = 0.0,x;
    printf("The sum of your numbers will be computed.\n");
    printf("Input some numbers: ");
    while(cnt <5)
    {
    scanf("%f",&x);
    sum = sum + x;
    cnt = cnt + 1;
    }
    printf("\n%s%12f\n\n sum:", sum);
    return 0;
    }

    -------

    perhaps this would be chicken feed for any C programmer...
    but i have no idea about this thing... and I just need assistance..
    and I wont forget any extended help... Thanks !!

    - [email protected]

  5. #5
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    ::edit::
    Deleted my message its of no use that you use 2 threads to get help on the same thing. The least you could of done is rephrase it or obfuscate it very much.

    Anyway you alrdy got some very decent help from the others in the other thread.
    Last edited by GanglyLamb; 06-22-2003 at 04:56 AM.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    60

    Hey !! Thanks !!

    Thanks for all your replies and suggestions...

    this is my first real step in programming... and there's no way I'll forget how gruelling this task was...

    A long time ago.. Algebra and Trigonometry were foreign languages to me,.. and now its just like the Alphabet Song...

    I guess.. its the same thing with any programming language..

    soon,.. I'll have my book about this topic... and I'll be posting queries on this message board.

    I learned a lot from you guys in 30 minutes... than with my boring professor in 5 hours of lecture... (without hands-on yet..)

    Thank You Dudes !!!

    -if you wish to comment more,.. please do so...

    Thanks !!!!!! /* [email protected] */

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by wickedclownz
    It should be...

    #include <stdio.h>
    #include <conio.h>

    int main( ) //space needed after function type
    {
    int cnt = 0;
    float sum,x;
    printf("The sum of your numbers will be computed.\n");
    printf("Input some numbers: ");

    while(cnt<5)
    {
    scanf("%f",&x);
    sum+=x;
    cnt++; //to increment by 1.. same as cnt=cnt+1 or cnt+=1
    }

    printf("\n\n\nSum: %12f", sum);
    getch();
    return 0; //space needed after the return keyword
    }
    I've highlighted two lines that represent a bug. I'll leave you to work out why.

    For those that aren't doing so: Please use code tags when posting code.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What language did they make Java in?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 07-03-2005, 04:18 PM
  2. Strange loop
    By D@rk_force in forum C++ Programming
    Replies: 22
    Last Post: 12-18-2004, 02:40 PM
  3. Language of choice after C++
    By gandalf_bar in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 06-15-2004, 01:20 AM
  4. Languages dying
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-29-2003, 10:08 AM
  5. Language Script..
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-30-2003, 06:48 AM