Need help with my program C++ - arrays

When i enter the five numbers for the second array it does not do anything. I need it to output five random numbers for the first array and five numbers assigned by a user.
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

void printarray(int printarray1[], int array_size);
void fillarray(int firstarray[], int array_size);
void printarray2(int printarray2[], int array_size);
void fillarray2(int secondarray[], int array_size);

int main()
{
int firstarray[5];
int secondarray[5];

//Fill array with random numbers
fillarray(firstarray, 5);
//Fill array with assigned numbers
fillarray2(secondarray, 5);

//Print first array
printarray(firstarray,5);
printarray(secondarray, 5);




system("pause");
return 0;
}


void printarray(int printarray1[], int array_size)
{
for (int n = 0; n < array_size; ++n)
cout << printarray1[n] << ' ';
cout << '\n';
}
void printarray2(int printarray2[], int array_size)
{
for (int n = 0; n < array_size; ++n)
cout << printarray2[n] << ' ';
cout << '\n';
}

void fillarray(int firstarray[], int array_size)
{
//Set random generator
srand(time(0));
for (int i = 0; i < array_size; ++i)
{
//Fill each item with random generated number from 1 to 25
firstarray[i] = rand() % 25 + 1;
}
}
void fillarray2(int secondarray[], int array_size)
{
int num1, num2, num3, num4, num5;
cout << "Please enter in 5 assigned numbers for the second array " ;
cin >> num1 >> num2 >> num3 >> num4 >> num5;
for (int i = 0; i < 5; ++i)
{
cin >> secondarray[5];
}
}
You have two inputs here - first the cin statement into 5 separate num variables which do nothing, then in the for loop. The for loop tries to input 5 times into secondarray[5], which is not a valid element (if array size is 5, elements are 0 through 4)

1
2
3
4
5
6
7
8
9
10
void fillarray2(int secondarray[], int array_size)
{
    int num1, num2, num3, num4, num5;
    cout << "Please enter in 5 assigned numbers for the second array " ;
    cin >> num1 >> num2 >> num3 >> num4 >> num5;
    for (int i = 0; i < 5; ++i)
    {
        cin >> secondarray[5];
    }
}
so should i remove one of the inputs
I would. I'd fix the for loop and stick with that.
Just that forloop right there isn't any other errors with the program besides that?
You don't really need two separate print functions, since they basically do the same thing. Unless the assignment asked you to have two? You only actually call one.
1
2
printarray(firstarray,5);
printarray(secondarray, 5);
Add a program that declares two arrays of 5 integers each. Provide one function to load one array with random numbers from 1 to 25, another to fill the other array with user supplied values, and a third function to print an array. The main function should declare the arrays and use the functions to fill each array and print them both. - this is what i have to do.
without print array how do i cout the secondarray
Last edited on
No, I meant I didn't think you needed this second print function, printarray2. Since the assignment asks for 3 functions, you don't need this fourth one.

1
2
3
4
5
6
void printarray2(int printarray2[], int array_size)
{
for (int n = 0; n < array_size; ++n)
cout << printarray2[n] << ' ';
cout << '\n';
}


Don't change how you've called printarray function on the two different arrays.

Last edited on
#include <cstdlib>
#include <iostream>
#include <ctime>

using namespace std;

void printarray(int printarray1[], int array_size);
void fillarray(int firstarray[], int array_size);
void fillarray2(int secondarray[], int array_size);

int main()
{
int firstarray[5];
int secondarray[5];

//Fill array with random numbers
fillarray(firstarray, 5);
//Fill array with assigned numbers
fillarray2(secondarray, 5);

//Print first array
printarray(firstarray, 5);
system("pause");
return 0;
}


void printarray(int printarray1[], int array_size)
{
for (int n = 0; n < array_size; ++n)
cout << printarray1[n] << ' ';
cout << '\n';
}

void fillarray(int firstarray[], int array_size)
{
//Set random generator
srand(time(0));
for (int i = 0; i < array_size; ++i)
{
//Fill each item with random generated number from 1 to 25
firstarray[i] = rand() % 25 + 1;
}
}
void fillarray2(int secondarray[], int array_size)
{
int (num1), (num2), (num3), (num4), (num5);
cout << "Please enter in 5 assigned numbers for the second array: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;

}
do i need a forloop for the fillarray2 at the bottom because it just repeats in the program when i have one like it would tell me to keep entering numbers i just want it to cout the secondarray and the first array has the random ones on a separate line
I would have fixed and kept the for loop in the fillarray2 function.

Don't change how you've called printarray function on the two different arrays.


You don't need two separate functions to print out the two different arrays, but you do need to call the one print function two times as you did.

1
2
printarray(firstarray,5);
printarray(secondarray, 5);
void fillarray2(int secondarray[], int array_size)
{
int num1, num2, num3, num4, num5;
cout << "Please enter in 5 assigned numbers for the second array: ";
cin >> num1 >> num2 >> num3 >> num4 >> num5;
for (int i = 0; i < array_size; ++i)
{
secondarray[i] = num1, num2, num3, num4, num5;
}

}
is this better for the forloop
No - something closer to what you did in the first fillarray function. You don't need a whole set of single num variables. The incrementing counter in the for loop allows you to easily access the different elements of the array. Also - the comma operator doesn't work the way you seem to expect it to there.

Add code into the loop to print a message and get input, then set the array element to that input value.

1
2
3
4
5
6
7
8
9
void fillarray2(int secondarray[], int array_size)
{
    for (int i = 0; i < array_size; ++i)
    {
        //print message to user to enter one number for the array
        //read input into single variable
        secondarray[i] = //the single variable you just read input into
    }
}
Last edited on
thanks i get it now! it repeats in the forloop. i was totally lost for a while.
Topic archived. No new replies allowed.