Basic array problems
Consider this data file: numbers.txt.
Write the following programs:
- min & max (solution: min & max)
- average (solution: average)
- standard deviation (solution: standard deviation)
Various array problems
Setting the context
Start with the following partial program:
#include <iostream>
using namespace std;
int main()
{
// read size n, allocate array of n doubles, read values into array
int n;
cin >> n;
double * A = new double[n];
for(int i = 0; i < n; i++)
cin >> A[i];
// Now what?
return 0;
}
... and add code after the "Now what?" comment to solve one
of the problems below for the array A of n elements created
in the first part of the code.
Problem 1: Find the index of the minimum element
Print out the index of the minimum element in the array. So, for example:
~/$ ./ex1a
4
34 12 8 29
minimum element is A[2] = 8.
solution
Problem 2: Printing the elements
Print elements of the array, separated by commas.
So, for example:
~/$ ./ex1b
4
34 12 8 29
A = [34, 12, 8, 29]
solution
[Mandatory Practice Problem] Problem 3: Splitting an array into two arrays
Split positive and negative. I actually want you to create two new arrays:
- Array
Apos: an array containing the positive elements of A
- Array
Aneg: an array containing the negative elements of A.
- Ignore 0's in A
Then, your program should print out each array as follows:
~/$ ./ex1d
10
0 1 2 -3 4 -5 -6 7 8 9
negative: -3 -5 -6
positive: 1 2 4 7 8 9
solution
Other problems
-
Create and print array of the "partial differences"
of consecutive elements in the array.
Here's an example of a sequence of four numbers along
with the sequence of partial differences (note that
there are only three elements in that sequence).
3 7 8 5
\_/ \_/ \_/
4 1 -3
A run of your program should look like this:
~/$ ./ex1c
4
14 16 8 29
2 -8 21
solution
- histograms (arrays of counters)
- masks (i.e. an array of bools), for
example to implement a simple hangman game.