Sorting and Arrays

Hi all. I am supposed to write a program that will read some number of floating point data into x and then compute the sum, the mean and the median of these data. Need help on my for loop equations. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#pragma warning(disable:4996)
#include <stdio.h>
#define SENTINEL -1

int fillArray(float []);
void printArray(float[], int, float);
float computeSum(float[], int);
float computeMean(float[], int);
void sortArray(float[], int);

void main()
{
    float theArray [20];
    int arraySize;
    float arraySum;
    float arrayMean;
    arraySize = fillArray(theArray)-1;
    arraySum = computeSum(theArray, arraySize);
    arrayMean = computeMean(theArray, arraySize);
    printArray(theArray, arraySize, arraySum, arrayMean);
    sortArray(theArray, arraySize);
    getchar();
    getchar();
}

int fillArray(float theArray[20])
{
    int counter = 0;
    float userInput;
    do{
       printf("Enter a value for the array (Index = [%d]:", counter);
       scanf("%f", &userInput);
       theArray[counter] = userInput;
       counter++;
       }
    
    while
    (
        userInput != SENTINEL
    );
    
    return counter;
}

float computeSum(float theArray[20], int arraySize)
{
    int i;
    float arraySum = 0;
    for(i=0, i<arraySum, i++)
    {
        arraySum = arraySum+theArray[1];
        return arraySum;
    }
}

float computeMean(float arraySum, int arraySize)
{
    float arrayMean;
    arrayMean = arraySum/arraySize;
    return arrayMean;
}
    
    
    
void printArray (float theArray[20], int arraySize)
{
    int i;
    printf("The array is as follows:\n");
    for(i=0, i<arraySize, i++)
        
            printf("%f", theArray[i]);
    printf("\n\nThe sum of the array is %2 if", arraySum);
    printf("\n\nThe mean of the array is %2 if", arrayMean);
    
}

void sortArray(float theArray[20], int arraySize)
{
    int i;
    int j;
    for(j=0, j<arraySize-1, j++)
    {
        for(j=0, j<arraySize, j++)
        {
            if (theArray[j]>theArray[j+1])
            {
                temp = theArray[j+1];
                theArray[j+1]= theArray[j];
                theArray[j] = temp
            }
        }
    }
    printf("\n\n");
    for(i=0, i<arraySize, i++)
        printf("%2 if", theArray[i]);
    
}
Last edited on
Ask a specific question pls
Sorry. I really do not know how to word it. I guess I am asking is why am I getting a red flag with all my for loop equations for example line 49 says expression result unused in regards to the "<" (using xcode). I am new to coding and have been simply replicating through the help of this website, http://codingbat.com/doc/java-array-loops.html, where I am using the below example as my guide.



1
2
3
4
// 2. allocate 100 Account objects, and store their pointers in the array
for (int i=0; i<accounts.length; i++) {
  accounts[i] = new Account();
}
for(i=0, i<arraySum, i++), use ; to separate for loop sections. Like in the example you are following.
Wow, slipped my mind. Thank you. Another question. At line 56, I am getting an error which states conflicting types for "computeMean". Is it because of the "[]" within the parenthesis at line 8 for the float?
yes
Now one last question. Now that I removed the "[]" within the parenthesis at line 8, line 19 now states an error "Passing 'float[20]' to a parameter of incompatible type 'float'. Not sure how to fix this without having the previous problem.
you were not supposed to remove the []. Instead to add it where it was not.
Actually, according to his code he should change line 19 instead.

arrayMean = computeMean(arraySum, arraySize);
Line 52 is in the wrong place. Since it's inside the loop, you are returning from the function after making just one pass through the loop.
Topic archived. No new replies allowed.