Arrays

I re did a code I had for an assignment. I need some help on how to get the random number generator to work on my code.

/ ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

const unsigned int RANDOM_ARRAY = 3;
// =================
int main() {

int ii;
int a[RANDOM_ARRAY];
int b[RANDOM_ARRAY];
int c[RANDOM_ARRAY];

ii = 0;

for (ii = 0; ii < 10; ii++) {

ii = rand() % 10 + 1;
ii = a[ii];



}


ii = 0;
while (ii < RANDOM_ARRAY) {
cout << "a[" << ii << "] = " << a[ii] << " ";
cout << "b[" << ii << "] = " << a[ii] << endl;
ii++;
}// while loop end

ii = 0;
while (ii < RANDOM_ARRAY) {

c[ii] = a[ii] + b[ii];
ii++;
}// while loop end

return 0;
}

I re did a code I had for an assignment. I need some help on how to get the random number generator to work on my code


Can you please give as a little more on info on how the code is suppose to behave.
Thanks
Design and implement a c++ program which, when executed by the computer, fills an array with randomly generated integer values between Min_Value and Max Value, which are constants. The array should hold n values, where n is a constant. The computer should then output the contents of the array and sum of array element values. Write and invoke a function to sum the elements in the array in the main function.
I think the following code solves the problem:

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

const int MinValue = 0;
const int MaxValue = 10;
const int ARRAY_SIZE = 5;

// generates a random value
int generateValue()
{
    // set seed value
    // produce integers between Min and Max values
    int randValue = std::rand() % (MinValue + MaxValue);
    return randValue;
}


// computes the sum of elements in the array
int sum(int *ptrArray, int size){
    int sum = 0;

    for(int i = 0; i < size; i++){
        sum += ptrArray[i];
    }

    return sum;
}


int main(){
    int array[ARRAY_SIZE];

    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        array[i] = generateValue();
        std::cout << "array[" << i << "] = " 
                  << array[i] << std::endl;
    }

    std::cout << "Sum of numbers in array is " << sum(array, ARRAY_SIZE) << std::endl;

    return 0;
}
Last edited on
sorry but i haven't learned to code using std:: could you possibly re do the code using namespace std;
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int MinValue = 0;
const int MaxValue = 10;
const int ARRAY_SIZE = 5;

// generates a random value
int generateValue()
{
    // set seed value
    // produce integers between Min and Max values
    int randValue = rand() % (MinValue + MaxValue);
    return randValue;
}


// computes the sum of elements in the array
int sum(int *ptrArray, int size){
    int sum = 0;

    for(int i = 0; i < size; i++){
        sum += ptrArray[i];
    }

    return sum;
}


int main(){
    int array[ARRAY_SIZE];

    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        array[i] = generateValue();
        cout << "array[" << i << "] = " 
                  << array[i] << endl;
    }

    cout << "Sum of numbers in array is " << sum(array, ARRAY_SIZE) << endl;

    return 0;
}
I got the code to work but there was a small problem. the random number always outputs 0 as the minimum and the maximum always outputs 10 I tried coding to find the minimum and maximum of the random numbers the compiler generates but i couldn't figure out to make it work properly. this is what i have so far

if (array[i] > MaxValue) {
MaxValue = array[i];
else (array[i] < MinValue)
MinValue = array[i];

cout << "MaxValue = " << MaxValue << endl;
cout << "MInValue = " << MinValue << endl;

I got the code to work but there was a small problem. the random number always outputs 0 as the minimum and the maximum always outputs 10 I tried coding to find the minimum and maximum of the random numbers the compiler generates but i couldn't figure out to make it work properly. this is what i have so far


I don't think you should be looking for the minimum and maximum numbers since they are supposed to be constants. Also the problem didn't say you had to look for them. But if you want to look for the maximum number in the array you could use the following
1
2
3
4
5
6
7
8
9
10
11
int findMax(int *array){
    int maxInArray = array[0];

    for(int i = 0; i < ARRAY_SIZE; i++){
        if(maxInArray < array[i]){
            maxInArray = array[i];
        }
    }

    return maxInArray;
}
Topic archived. No new replies allowed.