Functions and Arrays

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.

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
  #include<iostream>
#include<iomanip>
#include<fstream>
#include<string>



using namespace std;




int main ()
{
    int array1[100][5];// array that can hole 100 numbers for column 1-5
    int largest,smallest;


    fstream textfile;
    textfile,open("arrayfill.txt");

{

     for( int column=0; column<6;column++)
       cout << array[100][5] << " ";
        }

       cout << endl;





return 0;
}
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.


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
#include<iostream>


static const int COL = 5;
static const int ROW = 100;
using namespace 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.
Last edited on
This is what i got from your help but all it does is print out the whole array.




#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>


static const int col = 5;
static const int row = 100;
using namespace std;




int main ()
{
int array1[col][row]; // array of 100 rows and 5 columns


fstream textfile;
textfile.open("arrayfill.txt"); // my text file
textfile << "writing this to a file.\n";
textfile.close();


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
}
}


for(int i=0; i<col; i++){
for (int k = 0; k < row; k++){
cout << array[i][k] << " ";




}

cout << endl;
I don't think you need 2D Array for this. You can have an array that has 100 integers and then print that in 5 columns.

like so:

1
2
3
4
5
6
    for (int i = 0; i < size; i++) {
        if ( (i % 5) == 0)
	    std::cout << std::endl;

        std::cout << std::setw(5) << arr[i];
    }


And if you must have a 2D Array, then you will probably be looking at array with dimension as array[5][20] or so..
Last edited on
which for loop are you telling me to change ?
Good point teez!
Reformatted my code to for 1d array.

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
#include<iostream>


static const int SIZE = 100;
static const int ROW = 5;

using namespace 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.

Try bubble sort.
https://en.wikipedia.org/wiki/Bubble_sort

Pseudocode
1
2
3
4
5
6
7
8
9
10
11
12
13
14
procedure bubbleSort( A : list of sortable items )
   n = length(A)
   repeat 
     swapped = false
     for 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


or

Insertion sort.
https://en.wikipedia.org/wiki/Insertion_sort

Pseudocode
1
2
3
4
5
6
7
8
9
 for i = 1 to length(A) - 1
    x = A[i]
    j = i - 1
    while j >= 0 and A[j] > x
        A[j+1] = A[j]
        j = j - 1
    end while
    A[j+1] = x[3]
 end for



Ask us for help if you get stuck
:)
Best
Last edited on
i been trying to figure out bubblesorts but I'm not understanding do you have any other site or video i can read or watch to help me understand it.
Topic archived. No new replies allowed.