array help, fast!

This is a discussion on array help, fast! within the C Programming forums, part of the General Programming Boards category; i need to write a program that reads a sequence of natural numbers ending with '-1', calculates the average, and ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    array help, fast!

    i need to write a program that reads a sequence of natural numbers ending with '-1', calculates the average, and prints the firrence between the average and each number, one per line. there are at least 10 numbers, but no more than 40.

    e.g

    10
    20
    30
    40
    40
    60
    70
    80
    90
    101
    -1 (average is 50) gives

    40
    30
    20
    10
    0
    -10
    -20
    -30
    -40
    -51
    51

    i need serious help, i'm majourly stuck

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Well lets see:
    • Create an array
    • Read in the numbers
    • Sum them up
    • Divide by number of elements
    • Go through the array and calculate and display the difference
    • exit the program

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    the -1 is the problem, how do i get it to stop at -1

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    • read the number in
    • Compare the number against -1
    • If its -1 break
    • else keep going with the loop

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    this is what i have so far
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 40
    
    int main(void)
    {
    int a[40];
    int i=0;
    int c;
    int sum=0;
    
    while(1)
    {
    scanf("%d",&c);
    if(c>=0){
    a[i] =c;
    i++;
    }
    else if(c==-1){
    a[i]=c;
    break;}
    }
    
    
    while(a[i]!=-1)
    {
      sum=a[i]+a[i+1];
      i++;
    }
    sum=sum+-1;
    return 0;
    }
    correct?

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Some points:
    You defined MAX so use it
    first while condition should make sure you don't over run your array (check to make sure i is less then MAX)
    after your read you know how many items are in the array
    between the first and second loop the value of i doesn't change so it will never go into the second loop
    you still aren't calculating the average
    you still aren't calculating the difference
    you need consistant indentation

  7. #7
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    sorry i'm having rel problems summing the values, here is my code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 40
    
    int main(void)
    {
    int a[MAX];
    int i=0;
    int b=0;
    int c;
    int d;
    int sum=0;
    
    while(i<MAX)
    {
    scanf("%d",&c);
    if(c>=0){
    a[i] =c;
    i++;
    }
    else if(c==-1){
    a[i]=c;
    break;}
    }
    
    return 0;
    }
    where and how do i sum the values?

  8. #8
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Since you do seem to be trying (but your indention needs work)
    Code:
    int c =0; /* Using c now for indexing */
    while (c < i ) /* We really don't want to add the -1 as it just signals the end of data */
    {
      sum += a[c];
      c++;
    }
    to calculate average just divide sum by i. The trouble is that they both are integers and an int divided by an int produces an int. So if you want to get a decimal answer you'll need to cast one of them to a float or a double

  9. #9
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks for the help thantos, i was having a stupid moment, ive fixed my problem now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Reading a file into an array for fast access later
    By matsharp in forum C Programming
    Replies: 10
    Last Post: 08-03-2006, 02:42 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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