Arrays

I have to write 8 different arrays (listed below). I have written codes for the first 2 and they work but I'm having trouble with the other 6 arrays. Can anyone help?

1. Display the array
2. Calculate the average value of the array
3. Return the minimum value of the array
4. Return the position in the array where the minimum is stored
5. Allow the user to select a position and change the value stored
6. Allow the user to swap 2 values in the array
7. Change the values in the array to random values
8. Sort the array from greatest to smallest

So far, 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
// This is array 1.

int main()
{
const int array_size = 10;
int array1[array_size] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << "Display array: " << endl;
for (int i=0; i <array_size; i++)
{
cout << i << "\t" << array1[i] <<endl;
}

// This is the second

int sum = 0;
int average = 0;
int array2[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int i=0; i<10; i++)
{
sum += array2 [i];
}
average = sum/10;
cout << "The average is: " << average;

system("PAUSE")
return 0;


I'm stuck on the other 6 arrays. I've written multiple codes for the other arrays but cannot get them to work properly. If anyone can help, it would be greatly appreciated.
Last edited on
You have code outside the main function in your program. I'm absolutely sure this will not even run.
The code does run because I've tested it. But I'm having trouble writing codes for the other 6 arrays.
Last edited on
Where does it say that you have to create 8 arrays?
You have 8 tasks, but they all can get accomplished with one array like:

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

using namespace std;

const int array_size = 10;
int array1[array_size] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

void DisplayArray();
void CalcAverage();
void GetMinimum();

int main()
{
  
  //Task 1
  DisplayArray();
  
  // Task 2
  CalcAverage();
  
  // Task 3
  GetMinimum();

  system("PAUSE");
  
  return 0;
}

On what planet did you test it? You must be missing a curly brace or it will not compile. Having said that,

For #3:
1
2
3
4
For each item in your array:
    compare the value stored there
    If it is the lowest you have seen:
        save the value

For #4:

Use #3 and also save the position where you saved the lowest value.

The code does run because I've tested it.

That may be, but the code you posted does not compile.

Line 8: i is undefined.

Hint for array #3: Allocate a variable to hold the minval. Initialize minval to array3[0]. Iterate through the array. If array[i] < minval, then replace minval.
The code complies on the computer I am currently using. So I don't understand how to will not compile for anyone else. If that's the case and it makes it earier, change whatever is needed.
I clicked on the little "gear" symbol and tried to compile your code. This is what the online compiler returned:

1
2
3
4
5
6
7
In function 'int main()':
8:1: error: 'cout' was not declared in this scope
8:9: error: 'i' was not declared in this scope
8:34: error: 'endl' was not declared in this scope
21:1: error: 'cout' was not declared in this scope
23:15: error: 'system' was not declared in this scope
24:9: error: expected '}' at end of input
The code complies on the computer I am currently using

The code you posted is clearly not what you are compiling if your compiles are error free.
I have gone over the code and found what was missing from it. Sorry for the inconvenience. It should hopefully compile now.
Last edited on
For whom?
Topic archived. No new replies allowed.