I apologize profusely in advance for how long this is...I am so lost :( :( :(
This is due in 15 minutes, and I have been working on it since assigned, but still struggling. I don't have classmates or a way to contact my professor, as I am the only student taking it online.
Can anyone please help?? I am so lost.
I do not know how to output the last parts he wants me to show -- and I am not sure I am understanding his instructions correctly and following them right:
Here is my professor's assignment:
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 "I am now done with CSS1! :)"
For this, you must write the function definitions for the prototypes provided below (each function is worth 10 points...So, 10pts extra credit):
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); // a function that returns a random integer between min and max
Sample Program Run:
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
I am now done with CSS1! :)
*/
My code so far:
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
|
/*
#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 ( ); // a function that shows the message "I am now done with CSS1! :)
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++)
{
// NOTE: randvalue[i] = rand () % 3 + 5 is NOT correct. switch 3 and 5.
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;
/*firstMinusLast(int a[0], int size);*/
showBeforeIndex;
done;
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 a[5], int size )
{ /*
int counter = 0;
for(int i = 0; i < size; i++)
if(randint[i] == numsearch)
counter++;
return counter;
*/
int counter = 0;
int numseach = 5;
for(int i = 0; i < size; i++)
if(randint[i] == numsearch)
counter++;
std::cout << "\nThe number 5 appears " << counter <<" times.\n";
}
int firstMinusLast(int a[], int size)
{
return(a[0] - a[size-1]);
std::cout <<"\nThe difference between the first and last array elements is" << firstMinusLast << "\n";
}
void showBeforeIndex( int a [ ], int size, int index) // shows all array values before a specified index
{
for(int Ix = 0; Ix < index; Ix++)
cout << a[Ix] << " ";
} // a function that shows the message "I am now done with CSS1! :)
void done ( )
{
std::cout << "I am now done with CSS1! :)\n";
}
int randint(int min, int max);
|