Array with random numbers between 3 to 7. Then reverse them.

I am new to arrays and need some help with making an array that prints out 25 random numbers between 3 to 7, using simple functions. I have the array set up, but I am stuck on how to make the loop portion I think. I have also created the function prototypes/definitions that I want to use to return the answers. Here is an example of what I am going for.

Making an array of 25 random integers from 3 to 7!
Original array a [ ] = { 3, 7, 5, 6, 3, 4, 4, 3, 5, 5, 6, 5, 5, 7, 3, 5, 3, 6, 4, 5, 7, 4, 7, 3, 5 }
Reversed array a [ ] = { 5, 3, 7, 4, 7, 5, 4, 6, 3, 5, 3, 7, 5, 5, 6, 5, 5, 3, 4, 4, 3, 6, 5, 7, 3 }

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
  #include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showReverse ( int a[ ], int size ); // shows the array in reverse using the format "int a [ ] = { 7, 4, 3, 6, 5, ... , 4, 7, 3 } "

int main ()
{
    srand((int)time(NULL));
    int i=0;
    float randvalue[4000];



    randvalue[i] = rand () % 3 + 5; // random number between 3 to 7
    cout << "Making an array of 25 random integers from 3 to 7!" << endl;
    cout<<"Original array a [ ] = " <<randvalue[i] << endl;


    // Reversed array
    randvalue[i] = rand () % 3 + 5;
    cout<<"Reversed array a [ ] = " <<randvalue[i];

    return 0;
}

// Function definitions 
void showArray ( int a[ ], int size )
void showReverse ( int a[ ], int size )







Last edited on
You are on the right track so far. All you need to do now is loop through the array and assign a random value to each i-th element.

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
47
48
49
50
51
52
53
54
55
  #include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void showArray ( int a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "

int main ()
{
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25; // good to have a SIZE variable for arrays
    float randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(i; i < SIZE; i++)
    {
            // NOTE: randvalue[i] = rand () % 3 + 5 is NOT correct. switch 3 and 5.
            randvalue[i] = rand () % 5 + 3; // random number between 3 to 7
            
            // i will go from 0 to 3999, and thus, the statement above
            // will assign a random number to every element of the array. 
    }
     
     showArray(randvalue, SIZE); // pass the array and its SIZE to function

    // Reversed array
     // One way to reverse an array is to swap the first and last value, 
    // then swap the second and second-to-last value, 
    // then swap the third and third-to-last value, and so on...

    int j = SIZE-1; // initialize j to the last index
    i = 0;  // set i to the beginning index (i.e. index 0)

    while( i <= j)  // keep loop until i and j cross over
    {
        std::swap(randvalue[i], randvalue[j]); // swap the values at i-th and j-th index
        i++; // move i forwards
        j--; // move j backwards
    }

    showArray(randvalue, SIZE);
    // note that a showReverse() function is NOT needed. The showArray()
    // function is fine as it prints any array given to it. 


    return 0;
}

// Function definitions 
void showArray ( int a[ ], int size )
{
     for(int i = 0; i < size; i++) std::cout << a[i] << " ";
}
Last edited on
You got rid of my showReverse function? Also, I get float to int errors for void showArray
Yes, like I said in my comments, the showReverse() function is unnecessary. You can simply call the showArray() function both times.

To fix the errors, change the int in your prototype and function definition into float:
void showArray ( float a[ ], int size )
Last edited on
I still get and error: invalid types float [float] for array subscripts

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
47
48
49
50
51
52
53
54
55
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void showArray ( float a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "

int main ()
{
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25; // good to have a SIZE variable for arrays
    float randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(i; i < SIZE; i++)
    {
            // NOTE: randvalue[i] = rand () % 3 + 5 is NOT correct. switch 3 and 5.
            randvalue[i] = rand () % 5 + 3; // random number between 3 to 7

            // i will go from 0 to 3999, and thus, the statement above
            // will assign a random number to every element of the array.
    }

     showArray(randvalue, SIZE); // pass the array and its SIZE to function

    // Reversed array
     // One way to reverse an array is to swap the first and last value,
    // then swap the second and second-to-last value,
    // then swap the third and third-to-last value, and so on...

    int j = SIZE-1; // initialize j to the last index
    i = 0;  // set i to the beginning index (i.e. index 0)

    while( i <= j)  // keep loop until i and j cross over
    {
        std::swap(randvalue[i], randvalue[j]); // swap the values at i-th and j-th index
        i++; // move i forwards
        j--; // move j backwards
    }

    showArray(randvalue, SIZE);
    // note that a showReverse() function is NOT needed. The showArray()
    // function is fine as it prints any array given to it.


    return 0;
}

// Function definitions
void showArray ( float a[ ], int size )
{
     for(float i = 0; i < size; i++) std::cout << a[i] << " ";
}


line 54
Last edited on
Hi,

in line 17 you have error
for(i; i < SIZE; i++) should be just for(; i < SIZE; i++)
that is because you have i declared already

also in line 54 instead of float use int
@shadder: Thanks for that. My mistake.


@OP: Yes, please change the loop counter on line 54 to an int, not flloat.
Welcome :)
How would I make it more neat though like my post? Also, if I wanted to add the highest, lowest, and the sum, how would I do that? I have written the functions for it.

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
47
48
49
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void showArray ( float a[ ], int size ); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "

int main ()
{
    srand((int)time(NULL));
    int i=0;
    const int SIZE = 25; // good to have a SIZE variable for arrays
    float randvalue[SIZE];

    cout << "Making an array of 25 random integers from 3 to 7!" << endl;

    for(i; i < SIZE; i++)
    {
            
            randvalue[i] = rand () % 5 + 3; // random number between 3 to 7

            
    }

     showArray(randvalue, SIZE); 

    int j = SIZE-1; 
    i = 0;  

    while( i <= j)  
    {
        std::swap(randvalue[i], randvalue[j]); 
        i++; 
        j--; 
    }

    showArray(randvalue, SIZE);
   


    return 0;
}

// Function definitions
void showArray ( float a[ ], int size )
{
     for(int i = 0; i < size; i++) cout << a[i] << " ";
}
    


functions for the lowest, highest, and the sum. I want a function for these as well. Any tips?
int lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)
int sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array

Last edited on
int lowest ( int a[ ], int size ); // finds and returns the lowest value in the array (should be 7)
int highest ( int a[ ], int size ); // finds and returns the highest value in the array (should be 3)


try to do it by yourself, if you are stuck google it, anyways here are some link

http://www.cplusplus.com/forum/beginner/16878/
http://www.cppforschool.com/assignment/array-1d-sol/smallest-element-array.html

int sumArray ( int a[ ], int size ); // calculates and returns the sum of all values in the array

same goes for this
Topic archived. No new replies allowed.