Thread: C code for calculating total value in an array

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    7

    C code for calculating total value in an array

    give me the code for enter numbers
    i need to write codes for input number to an array and count the total value of numbers stored in the array



    i'm unable to write input and calculate total of the values of array

    give me the code for enter 5 numbers and calculate total of 4 th and 5th number

    but i write code for calutale total for an initialized array but this code display all values need to display just total is 100
    Code:
    int main()
    {
    
    
    
    
    int number[4];
    number[0]=100;
    number[1]=15;
    number[2]=23; 
    number[3]= number[0]+number[1]+number[2];
    int index;
    for(index=0; index<4; index++)
    {
    printf( "total is= %d\n",number[index]); 
    return 0;
    }
    }

  2. #2
    spaghetticode
    Guest
    Quote Originally Posted by Fn00 View Post
    give me the code for enter numbers
    No.

    Quote Originally Posted by Fn00 View Post
    i'm unable to write input and calculate total of the values of array
    Please tell us more specific what you are having problems with.

    Quote Originally Posted by Fn00 View Post
    but i write code for calutale total for an initialized array but this code display all values need to display just total is 100
    Try to explain in your own words what you think this part of your code does:

    Code:
    ...
    for (index=0; index<4; index++) {
        printf( "total is= %d\n",number[index]); 
    }
    ...

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    7
    sorry i ust post without complete

    i want to write code for this program

    array 'numbers' can hold up to 5 values( int numbers [5]
    input 5 values and calculate
    1) the total input numbers
    2) calculate the total of 2 and 4 th element( total of the value of numbers[1] and value of numbers[2]

    i cant get idea of the loop of how to calculate total values

    but i just wrote code for calculating total for a value assigned array but total not showing

  4. #4
    Registered User
    Join Date
    Apr 2015
    Posts
    7
    for (index=0; index<4; index++) {
    printf( "total is= %d\n",number[index]);

    i just tried for loop for this

  5. #5
    spaghetticode
    Guest
    I understood the assignment and I understood what you've been trying to do. But you did something different. That's why I asked you to tell us in your own words how you think your for-loop works.

    Actually you don't seem to have any clue what you are doing. This is most basic stuff you'll find in any decent tutorial or textbook, probably even in the not so decent ones.

    Okay... where to start? What you are doing by that loop is, you go through your array and print out each single element. That's not what you want. You want the final result. This result you need to compute beforehand. You actually did this by the line

    Code:
    number[3]= number[0]+number[1]+number[2];
    but that's surely not what the assignment intended.

    You'll need a separate variable to store the sum in. You'll need a for-loop to go through your array and add each element to your sum-variable. And you'll need a single printf-statement without any loop to print out the result. Don't worry about user input for now, you should get the summation right beforehand.

    Can you come up with something after these hints?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array add total rows and total colms
    By gameover6005 in forum C Programming
    Replies: 1
    Last Post: 04-16-2013, 11:46 PM
  2. File I/O calculating total help
    By rfoor in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2010, 12:52 PM
  3. Replies: 5
    Last Post: 01-23-2007, 05:52 PM
  4. Replies: 6
    Last Post: 01-08-2006, 02:49 PM
  5. average of total using array
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 05:27 PM