### Understand array instructions

Hello,

I'm trying to work out these instructions -

1) The user is prompted to enter a positive integer, say n.
2) As long as n is between 10 and 30, the program will
a. Generate n number of random integers which will be no greater
than 99.
b. Print all these random integers 10 numbers per line and 6 spaces
per number.
c. Find and print the maximum and the minimum of these numbers.
Write two functions, FindMaximum and FindMinum. Each function
will return an integer and accept the following two parameters:
integer array and the size of the array.

For which I have:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` `````` #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int n; int srand (time (NULL)); int i; int x; int c = 0; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (10 > n < 30) { for (c = 0, i = 0; i < n; i++) { x = rand()%100; cout << setw (6) << x; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } cout << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````

I can't figure out part c though. I understand arrays somewhat, but not what the teacher is asking me to do with the information. Specifically this -

'Each function will return an integer and accept the following two parameters:
integer array and the size of the array.'

I think the program works for 1) and 2)a.b.d. but not c
I would appreciate any help.
Last edited on
How familiar are you with functions?
So, like if I wrote the functions -

int FindMaximum (int x, int n)
int FindMinimum (int x, int n)

I'm not really sure how to pass data to or from an array though.

'Each function
will return an integer and accept the following two parameters:
integer array and the size of the array.'
You can pass arrays to functions but only by reference. You can't actually pass the array itself. The way you do this is:

 ``12`` ``````int array[10] somefunction(array, 10)``````

Remember, the name of the array is actually a pointer to the first element of the array. As such your function prototype should look like:

 ``12`` ``````void somefunction (int * array, int size) // or void somefunction (int array[], int size)``````

Both prototypes are the same. Note, you can omit the names in the prototype if you wish, just like for regular types. Your function definition must include the names however and could look like this:

 ``1234`` `````` void somefunction (int array[], int size) { //code here }``````

If you want to return an array, say after using the function to create one, it would look like this, say with a variable passed to the function saying how large the array should be:

 ``12345`` ``````int * createarray (int size) { int * p = new int[size]; return p; }``````

If you use a function to create an array I believe you must use new, or else it'll create the array on the stack which means it will be deleted after the function exits. Using new will create it on the heap so it'll stay after the function exits, but you'll still have access to it since you returned the address of it.

BTW, you comparison expression in the while loop at line is invalid syntax. It should look like:

`while (n > 10 && n < 30)`

Also on line 30, c is already initialized to 0, so you don't really need to do it again. It doesn't hurt anything, it's just a bit of unnecessary code.
Last edited on
Well the c has to equal 0 in the 'if' statement because it becomes true and then 'c' can never equal ten again unless it starts back at 0. The first c = 0 is just to start with.

So, if I run the program and put in '20' for 'n,' then it creates 20 random numbers. How do I get all that information to go into the function. A single number, sure, I can pass that along, but all those numbers? I suppose I would have to make those 20 numbers into an array, and then I could just pass the array info to the functions. I don't know how to make those numbers into an array though.

Thanks for the help so far.
Simply create an array of size n after the user inputs n. Then instead of x= rand on line 32, you could use the array like:

`array[i] = rand() %100;`

Line 33 can then simply use the array the same way to output it like:

`cout << setw (6) << array[i];`

As for the having to equal 0, yes in the IF statement, I was talking about the initialization the FOR statement being unneeded since you reset it to 0 in the IF statement anyway and it was already initialized to 0 when you declared it. Re-assigning 0 to c in the FOR's initialization expression does nothing that isn't already handled elsewhere.
What's array[i] supposed to do? Is that supposed to be sending data to a function named array? What's 'i' for?

edit -

is that because it gets +1 for each number, and then that makes the array 1 larger each time? So, i = 1, the array is one number large - i = 2, the array has two values, etc.

Oh, and then that fills in the array of size n. I'll give that a try.

Ok, so I tried int array[n], and my compiler says that [n] has be a constant value. I'm using microsoft visual studio 2010. Not sure if that has anything to do with it. I mean, the array could just be 29 values I suppose. That's the max value a person can enter for 'n.'

Ok, so I do have to have the program still do what it did before - output the info, but also put that info into an array, and pass it to a function. Or, pass the info to a function and that function places that info in the array...I guess.

I currently have this -

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` `````` #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int n; int srand (time (NULL)); int i; int x; int c; int array[29]; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { x = rand()%100; cout << setw (6) << x; array[i] = rand() %100; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } cout << endl; cout << setw (6) << array[i]; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````

That array is just a bunch of gibberish for whatever reason. Each random number doesn't seem to be going into the array, and setw (6) doesn't seem to apply for this - cout << setw (6) << array[i];
Last edited on
Ok, so I have this

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` `````` #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int n; int array[29]; int srand (time (NULL)); int i; int x; int c; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } cout << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````

How do I get the information from the array into a function? Do I send the data one at a time, as it generates in the for loop? Or, do I wait until the for loop finishes and then send the data? I know how to get variables to go into a function, but not array data. Once it's there, I think it would be a simple process of writing an if statement to check if each subsequent number is lower or higher. Just gotta figure out how to get all those numbers, which I guess are a part of 'array,' to go into a function.
Last edited on
Well, here's what I have so far -

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` `````` #include "stdafx.h" #include #include #include #include using namespace std; int findmin (int array[], int size) { int i = 0; int min=100; while (array[i] <= size) { if (array[i] < min) min = array[i]; i++; } return min; } int _tmain(int argc, _TCHAR* argv[]) { int n; int array[29]; int srand (time (NULL)); int i; int x; int c; int min; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } cout << endl; cout << "The minimum is " << min << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; }``````

I hope you can follow the logic. It doesn't work, but I don't really know what to do.

1>(32): warning C4244: 'initializing' : conversion from 'time_t' to 'int', possible loss of data
1>(34): warning C4101: 'x' : unreferenced local variable
1>(62): warning C4700: uninitialized local variable 'min' used

Just trying to get that array data into a function, and then have the function check for minimum and maximum numbers. I mean, yah I can do it a whole hell of a lot simpler by not even using the arrays or functions at all, and just applying an 'if' statement to the numbers as they come out, but this is how we were instructed to do the problem.
Last edited on
Line 15, you are saying that while each the value in each element of the array is less then size. So basically you are saying if the random number you entered into array[i] is less then size. Not sure if that's what you wanted. What I think you wanted to do was iterate through the array and compare each element to the value of min, if min is greater replace it with the value in array[i] then increment i? If so you simply use ` while (i < size)` This, with the increment i++ will iterate through each element and do the rest of the code . The only other issue there is you have the i++ INSIDE the if loop, so if array[i] > min the rest of the code inside won't run. That includes the i++, so the if will keep evaluating the same element against min. Try moving the i++ just after the close brace for the if loop.
It says (32): warning C4244: 'initializing' : conversion from 'time_t' to 'int', possible loss of data' for this line -

int srand (time (NULL));

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include "stdafx.h" #include #include #include #include using namespace std; int findmin (int array[], int n) { int i = 0; int min=100; while (i <= n) { if (array[i] < min) min = array[i]; i++; } return min; } int _tmain(int argc, _TCHAR* argv[]) { int n; int array[29]; int srand (time (NULL)); int i; int c; int min = 0; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } findmin(array, n); cout << endl; cout << "The minimum is " << min << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````
Last edited on
I think the error may have something to do with my array size. I don't really know though. The program won't run until the error is resolved.
Remove the "int" in front of srand(time(NULL)).
Still says - warning C4244: 'argument' : conversion from 'time_t' to 'unsigned int', possible loss of data

points to line 27. I removed int, but there's no difference.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566`` ``````#include "stdafx.h" #include #include #include #include using namespace std; int findmin (int array[], int n) { int i = 0; int min=100; while (i <= n) { if (array[i] < min) min = array[i]; i++; } return min; } int _tmain(int argc, _TCHAR* argv[]) { int n; srand (time (NULL)); int i; int c; int min = 0; int array[29]; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } findmin(array, n); cout << endl; cout << "The minimum is " << min << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````
Ok, so I found piece of code

srand ( (unsigned int)time(NULL) );

that works for whatever reason. stackoverflow.com said:
'That's because on your system, time_t is a larger integer type than unsigned int.
time() returns a time_t which is probably a 64-bit integer.
srand() wants an unsigned int which is probably a 32-bit integer.'

Which, I have absolutely no clue what that means, but the code works.

Now, I'm still stuck with the issue of getting the array info into the function, having that check to see if it can find a minimum number, and then providing that minimum number to the main function.

These are the instructions: . Find and print the maximum and the minimum of these numbers.
Write two functions, FindMaximum and FindMinum. Each function
will return an integer and accept the following two parameters:
integer array and the size of the array.

I'm just starting off with findminimum. I don't know how to get the data of an array into a function. While it's in a function, I don't know how to check each value to see if it's lesser or higher than the previous, and I don't know how to provide that value back to the main function. I think I've spent about 5 hours so far reading on how to do that, and I'm not really any closer. Just wanted to let you know that I'm trying.
Last edited on
So, I have the array being created, and I'm trying to pass the info to a function with.

findmin(array, n);

The function is

int findmin (int array[], int n)

Am I formatting it correctly?
Alright so after fudging around for a long while, I got it all to work, except for that it's not returning 'min' back to the main function.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` `````` #include "stdafx.h" #include #include #include #include using namespace std; int findmin (int array[], int n) { int i = 0; int min = 100; while (i < n) { if (array[i] < min) min = array[i]; i++; } return min; } int _tmain(int argc, _TCHAR* argv[]) { int n; srand ( (unsigned int)time(NULL) ); int i; int c; float min; int array[29]; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } findmin(array, n); cout << endl; cout << "The minimum is " << min << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; } ``````
Line 58, your findmin functions returns min, but it has no where to put it. You would have to make it ` min = findmin(array,n)` since I assume you want min declared in the main() to hold this value since you are outputting it a few lines later.
Ok, I put it below where 'n' and 'array' get values assigned to them and it seems to have worked.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include "stdafx.h" #include #include #include #include using namespace std; int findmin (int array[], int n) { int i = 0; int min = 100; while (i < n) { if (array[i] < min) min = array[i]; i++; } return min; } int _tmain(int argc, _TCHAR* argv[]) { int n; srand ( (unsigned int)time(NULL) ); int i; int c; int array[29]; cout << "Please enter a positive integer between 10 and 30 --> "; cin >> n; while (n > 10 && n < 30) { for (c = 0, i = 0; i < n; i++) { array[i] = rand()%100; cout << setw (6) << array[i]; //checks for 10 numbers, and if there are 10, goes to the next line c++; if (c == 10) { cout << endl; c = 0; } } findmin(array, n); cout << endl; int min = findmin (array, n); cout << "The minimum is " << min << endl; cout << "\nPlease enter a positive integer between 10 and 30 --> "; cin >> n; } return 0; }``````

Thanks a bunch Raezzor
Last edited on
Topic archived. No new replies allowed.