Thread: Beginner Question!

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    24

    Beginner Question!

    Im taking a C programming class in college and ive never had any previous experience programming. I got this assignment and i dont know how to do it. The assignment is Create a C file named numbers.c that accept as input from the keyboard 5 numbers. Calculate the following:Sum, Product and Average. Ok i went to my teacher for help and all she would tell me is that i am computing the sum before you receive the input from the
    keyboard. HOw do i fix this? Here is my code:



    #include <stdio.h>
    int main()

    {

    int x,y,sum;
    sum = x + y;

    printf("Enter Two Numbers:");

    scanf("%i %i",&x,&y);

    printf("Sum:%i",sum);

    return 0;
    }

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Welcome to the board. Please read this thread
    << !! Posting Code? Read this First !! >>

    as for your problem its fairly simple to fix:
    Code:
    #include <stdio.h> 
    int main(void) 
    
    {
    
    int x,y,sum;
    
    printf("Enter Two Numbers:");
    
    scanf("%i %i",&x,&y);
    sum = x + y;
    printf("Sum:%i",sum);
    
    return 0;
    }

  3. #3
    Ah yes,

    Calculations are somewhat picky in C, or, shall I say, entirely. Analogy:

    Question: There are x people in room A, and y people in room B, how many people are in all?

    First things first:
    · Find x and y [C: scanf()]
    · Add the two [x + y]
    · Result of how many people [x+y]

    Do this in the wrong order, and a few problems occur.

    · Add the two [x and y are just variable fronts, and aren't set yet]
    · Find x and y [a little to late now]
    · Result is still unknown


    Hope this makes sense,
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    24

    Thanks

    thanks so much...i was trying to figure that out forever. Thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM