This is my first time really coding and i'm having a very hard time. My assignment is to use an array of integers an print the array out in five columns, find the largest and smallest value-print with labels, print the array out in five column starting at the highest index and going to the smallest index, calculate the mean, standard deviation, variance of the set of values, and print the answer calculated in step with appropriate labels. This is what i have.
So I think you need to start with a sort function...
Then then start working on the math stuff.
I would write the program out this way
and start working on one problem at a time.
#include<iostream>
staticconstint COL = 5;
staticconstint ROW = 100;
usingnamespace std;
int main() {
// 1. use an array of integers
int array1[COL][ROW];
// filling array with loop (I do not have your text file)
int value = 0;
for (int i = 0; i < COL; i++) {
for (int k = 0; k < ROW; k++) {
array1[i][k] = rand() % 100; // for random values 0-99
}
}
// 2. print the array out in five column starting at the highest index
// first sort...
// you need a sort function.
// try bubble sort.
// https://en.wikipedia.org/wiki/Bubble_sort#Pseudocode_implementation
// then print...
// printing array only.
for (int i = 0; i < COL; i++) {
for (int k = 0; k < ROW; k++) {
cout << array1[i][k] << " ";
}
cout << endl;
}
// 3. calculate the mean
// loop over array.
// do the math
// save answer to variables
// 4. standard deviation
// loop over array
// do the math
// save answer to variables
// 5. variance of the set of values
// loop over array
// do the math
// save answer to variables
// 6. print the answer calculated in above with appropriate labels.
return 0;
}
So start with Bubble sort (or any one you choose) and post any questions you have here.
#include<iostream>
staticconstint SIZE = 100;
staticconstint ROW = 5;
usingnamespace std;
int main() {
// 1. use an array of integers
int array1[SIZE];
// filling array with loop (I do not have your text file)
for (int i = 0; i < SIZE; i++) {
array1[i] = rand() % 100; // for random values 0-99
}
// 2. print the array out in five column starting at the highest index
// first sort...
// you need a sort function.
// then print...
// printing array only.
int k = 1;
for (int i = 0; i < SIZE; i++, k++) {
cout << array1[i] << " ";
// every 5 iterations
if (k == ROW) {
// print new line
cout << endl;
// reset
k = 0;
}
}
// 3. calculate the mean
// loop over array.
// do the math
// save answer to variables
// 4. standard deviation
// loop over array
// do the math
// save answer to variables
// 5. variance of the set of values
// loop over array
// do the math
// save answer to variables
// 6. print the answer calculated in above with appropriate labels.
return 0;
}
@jcelestin you should look into how to sort an array.
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = falsefor i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure