Thread: Adding numbers (Arrays)

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Adding numbers (Arrays)

    Hey guys I am a beginner in coding and I am stuck on some homework. I have done some of it, but I do not know how to a part. The instructions are as follows:
     Declare an integer variable that will hold a sum and give it an initial
    value of zero.
     Declare a ten element integer array named myarray.
     Using a printf/scanf pair in a for loop, input integers into the ten
    elements of the array.
     Use a second for loop to compute the sum of the ten values, storing
    the result in the variable you created in the first step.
     Use a single printf to display "The sum is: " followed by the value
    your program computed as the sum. End the line with a newline
    character. Notice the space character following the colon. This
    provides space that makes reading your output a little easier.

    I have this so far:
    Code:
    #include <stdio.h>#include <conio.h>
    
    
    void main()
    {
        int myarray[10] = {1,2,3,4,5,6,7,8,9,9};
        int i;
        int total=0;
    
    
        for(i=0; i<10; i++)
        {
            total +=myarray[i];
        }
    
    
        printf("The toal is %d", total);
    }
    How do I do the third bullet, where the user can input 10 intgers?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Using a printf/scanf pair in a for loop
    Well, you have the "for()" loop. Now try adding a print statement (to prompt for user input) and a scan statement (to receive user input). Do you have experience scanning integers with "scanf()"?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding numbers with arrays
    By Oliveira in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 10:25 PM
  2. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  3. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 03:36 PM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  5. Adding two numbers
    By Maybe in forum C Programming
    Replies: 2
    Last Post: 03-11-2003, 01:55 PM