Arrays Program for my Birthday! Help me with the code?



"Write a program that makes an array of 25 random integers each from 3 to 7 (use your function randint() to generate these numbers).

Your program:

A) declares the array in the main (you are not allowed to use global variables!),

B) initializes the above array to random values 3 to 7 using your randint() function

C) and then calls the functions with the provided prototypes to do the following:

show the original array,

show the array in reverse,

shows the lowest value in the array,

shows the highest value in the array,

shows the sum of all numbers in the array,

shows the average of all number in the array

shows how many times the number 5 appears in the array

shows the difference between the First and Last elements of the array

shows the elements of the array before a specified index

shows the message "Now it's my birthday! :)"

For this, you must write the function definitions for the prototypes provided below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void showArray ( int a[ ], int size );

void showReverse ( int a[ ], int size );

int  lowest ( int a[ ], int size );                     

int  highest ( int a[ ], int size );                

int  sumArray ( int a[ ], int size );                                  

float averageVal ( int a[ ], int size );                                  

int count5 ( int a[ ], int size );                                                

int firstMinusLast ( int a[ ], int size );                                  

void showBeforeIndex( int a [ ], int size, int index);          

void done ( );  // a function that shows the message "Now it's my birthday! :)**"

int randint(int min, int max);   


WHAT A SAMPLE PROGRAM RUN SHOULD LOOK LIKE**:**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 }

Lowest value is 3

Highest value is 7

The sum of all array elements is 120

The average of all array values is 4.8

The number 5 appears 8 times.

The difference between the first and last array elements is -2

The array values before Index 3 are 3, 7, 5

Now it's my birthday! :)" 


Last edited on
Your code is trying to define functions inside other functions. You're trying to define lots of functions inside main. This will not compile. Get your barebones program compiling first.
begin by making a main() with int a[] = { 3, 7, 5 ... }; , start implementing showArray() and call it from main()
So far I have this -- but not sure where to go. SUPER noob...so pretty lost. I understood everything up until arrays for some reason o.O Could be from lack of sleep, so brain is slow.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    
    #include <iostream>
    #include <stdlib.h>
    #include <cstdlib>
    #include <time.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  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
    float averageVal ( int a[ ], int size );                                        // calculates and returns the average of all values in the array
    int count5 ( int a[ ], int size );                                                 // returns how many times the number 5 appears in the array
    int firstMinusLast ( int a[ ], int size );                                    // returns the difference between the First Array Element - Last Array Element
    void showBeforeIndex( int a [ ], int size, int index);            // shows all array values before a specified index
    void done ( );                                                                           // a function that shows the message "I am now done with CSS1! :)
    int randint(int min, int max);


    int main()
    {
        cout << "Making an array of 25 random integers from 3 to 7!\n";  
        showArray    
        int array[25] = { 3 };

	for (int a = 1; a < 25; j++)
	{
		array[a] = 3 + rand() % 5;
	}

	//Print Array
	for (int i = 0; i < 100; i++)
	{
		cout << "Number: " << i << " " << array[i] << endl;
	}

	system("PAUSE");
	return 0;   
        done();
        randint(3,7);
        system ("pause");
        return 0;
    }

    int randint(int min, int max)
    {
        srand(time(0));
        int randomNum = min + (rand() % (max - min + 1));
        
    }


    void showArray (int a[], int size )  
    {
        cout << "Original array a [ ] = " << int a [];

        for (int a = 1; a < 25; a++)
        {
                    array[a] = 3+ rand() % 5;
         }

    }

    void showReverse (int a[], int size) 
    {

    }

    int lowest (int a[], int size)
    {
        
    }

    int highest (int a[], int size)
    {

    }

    int sumArray ( int a[], int size)
    {

    }

    float averageVal (int a[], int size)
    {

    }

    int count5 (int a[], int size)
    {

    }

    int firstMinusLast (int a[], int size)
    {

    }

    void showBeforeIndex( int a[], int size, int index)
    {

    }

    void done ()
    {
        cout << "Now it is my birthday! :)\n";
    }
ok let's examine your first few lines of main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cout << "Making an array of 25 random integers from 3 to 7!\n";  
showArray                          // This should cause a syntax error
int array[25] = { 3 };             // Array with 25 spaces.  *each one* initialized to value 3

for (int a = 1; a < 25; j++)       // From 1 to 24   (note your array goes from index 0 to 24). 
                                   // Also use the variable 'i' for indexing, since it's common.  
                                   // For some reason you're incrementing a non-existent variable 'j' here...?
{
    array[a] = 3 + rand() % 5;     // Looks good, 3..7 as intended
}

// These next lines are erroneous.  Firstly, you're accessing indices out of 
//   the 0..24 range.  Secondly, all this should be in showArray() method instead.

//Print Array
for (int i = 0; i < 100; i++)      
{
    cout << "Number: " << i << " " << array[i] << endl;
}


Added some comments. (Referring to line numbers from this post)
Line 3 should just be int array[25]; -- setting aside space without initializing anything, since your next lines will do the filling.
Line 5 should start at index 0. Arrays are zero-based. Check your indexing variable.
Lines 16-19 should be deleted. Call your show method instead -- this one should be doing the printing work.

Lot of syntax errors. I didn't try running it, but can see variables that don't exist and lots of methods that claim to return int but don't actually do so. For the stub methods slated to return an integer, add return -1; so that they do the bare minimum to compile.

Please try to correct all syntax errors and post reply with updated code/further questions.

For showArray() , make use of all parameters passed in to the method (why have them otherwise?) and remember what I said about zero-based above.
Last edited on
Updated code -- this time I am SO close to being done...but stuck at "firstminusLast" onward. Before this point it showed no errors, but now does. Anyone mind showing me the correct code for this?

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
    #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 );

    int  lowest ( int a[ ], int size );

    int highest ( int a[ ], int size );

    int  sumArray ( int a[ ], int size );

    float averageVal ( int a[ ], int size );

    int count5 ( int a[ ], int size );                  // returns how many times the number 5 appears in the array

    int firstMinusLast ( int a[ ], int size );                   // returns the difference between the First Array Element - Last Array Element

    void showBeforeIndex( int a [ ], int size, int index);          // shows all array values before a specified index

    void done ( );      

    int randint(int min, int max);                 // a function that returns a random integer between min and max


    int main ()
    {
        srand((int)time(NULL));
        int i=0;
        const int size = 25; // size variable for arrays
        int randint[size], lowest, highest;


        cout << "Making an array of 25 random integers from 3 to 7!\n";
        cout << "\n";
        cout << "Original array a [ ] = {";

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

         showArray(randint, size); // pass the array and its SIZE to function
         cout << "}\n";

        // 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...
        cout << "\n" << "Reversed array a [ ] = { ";

        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(randint[i], randint[j]); // swap the values at i-th and j-th index
            i++; // move i forwards
            j--; // move j backwards
        }

        showReverse(randint, size);
        cout << "}\n";

        lowest=randint[0];
        highest=randint[0];

        for (i = 0; i < size; i++)
        {
            if(randint[i]<lowest)
                lowest=randint[i];
            if(randint[i]>highest)
                highest=randint[i];
        }

        cout<<"\nLowest value is : "<<lowest <<"\n";
        cout<<"\nHighest value is : "<<highest <<"\n";


        int sum=0;


        for (int a=0; a<size; a++)
        {
			sum+=randint[a];
        }

        cout << "\nThe sum of all array elements is " << sum << endl;

        float average=sum/size;

        cout << "\nThe average of all array values is " << average << endl;


        int numsearch = 5;

        int counter = 0;
        for(int i = 0; i < size; i++)
            if(randint[i] == numsearch)
                counter++;
        std::cout << "\nThe number 5 appears " << counter <<" times.\n";

        std::cout << firstMinusLast;

        return 0;
    }

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

    void showReverse ( int a[ ], int size )
    {
         for(int i = 0; i < size; i++) std::cout << a[i] << " ";

    }

    int count5(int numsearch, int randint[], int size)
    {
        int counter = 0;
        for(int a = 0; a < size; a++)
            if(randint[a] == numsearch)
                counter++;
        return counter;
    }

    int first (int size)  /// find the first digit
        {
            while (size >=25)
                size /= 25;

            return size;
        }

    int last (int size)  /// find the first digit
        {
            return (size%25);
        }

    int firstMinusLast ( int a[ ], int size )
    {
        int first = -1, last = -1;
        for (int i=0; i<size; i++)
    {
        if (a != rand[i])
            continue;
        if (first == -1)
            first = i;
        last = i;
    }
    if (first != -1)
        cout << "First minus last = " << first-last;

    }

/* SAMPLE RUN:
ERRORS – will not compile; errors say: 
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
|In function 'int firstMinusLast(int*, int)'|
|214|warning: pointer to a function used in arithmetic [-Wpointer-arith]|
|214|error: comparison between distinct pointer types 'int*' and 'int (__attribute__((__cdecl__)) *)()' lacks a cast [-fpermissive]|
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
*/


Last edited on
At the end of main(), you have
std::cout << firstMinusLast;

Since firstMinusLast is a function, not sure what you expected to happen by trying to output it with just the identifier and not a function call with parentheses firstMinusLast(...)

But the function itself, which should be computing the difference between first element and last element, is going completely in the wrong direction:
int firstMinusLast ( int a[ ], int size )
{
int first = -1, last = -1;
for (int i=0; i<size; i++)
{
if (a != rand[i])
continue;
if (first == -1)
first = i;
last = i;
}
if (first != -1)
cout << "First minus last = " << first-last;

}


1. How do you access the first element in an array?
2. How, knowing the size, do you access the last element in the array?

Breathe, grab a coffee, and I think you'll realize what to do :D
Topic archived. No new replies allowed.