Thread: Averaging the values from multiple arrays

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

    Question Averaging the values from multiple arrays

    Averaging the values from multiple arrays. For this assignment you will implement a C function that modifies the elements in an array of integers. To start you create three separate 32bit integer arrays of 5 elements each. Next you populate each array using the values listed below. There are 20 values listed for each of three arrays named A1, A2, A3
    This array controls the order in which the element in the first three arrays are to be averaged. For example the first number in the control arrays is 0, therefore using the data from above you would average 32, 30 and the 31 then print the result. Then move on to the second element in the control array or the value of 19 (this last entry in the arrays A1, A2, and A3). For the second number in the control array you would average 2, 6, and 1 the print the result.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int i;
        int sum = 0;
        
        int A1[20]={32,12,9,4,88,65,87,87,75,7,86,86,963,84,37,9,48,8,88,2};
        int A2[20]={30,7,0,6,434,6547,6,889,959,346,6,8,2,27,89,8,6,3,3,6};
        int A3[20]={31,5,5,7,8,1,768,436,6,2,9785,111,404,238,2,4,3,9,96,1};
    
    
        int array[20]={0,19,17,15,13,11,2,4,6,8,10,1,3,5,18,16,7,9,12,14};
    
    
        for(i=array;i<array;i++){
            sum = sum + (A1+A2+A3) [i];
        }
    
    
        printf("Average of Arrays", sum);
    
    
        return 0;
    }
    I'm lost on how to do for loop for multiple array... and he did not even tech that in class

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your initial for loop should be
    for ( i = 0 ; i < 19 ; i++ )

    > For example the first number in the control arrays is 0, therefore using the data from above you would average 32, 30 and the 31 then print the result.
    > Then move on to the second element in the control array or the value of 19 (this last entry in the arrays A1, A2, and A3).
    > For the second number in the control array you would average 2, 6, and 1 the print the result.

    You then want a statement like this, to get
    int subscript = array[i];

    From there, you use things like
    A1[subscript]
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-01-2014, 07:48 AM
  2. Replies: 3
    Last Post: 12-07-2011, 07:11 PM
  3. test to see if multiple values are the same
    By squeaker in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 01:45 AM
  4. Returning Multiple Values
    By StarOrbs in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2005, 01:00 PM
  5. Replies: 2
    Last Post: 02-23-2004, 06:34 AM

Tags for this Thread