I am creating a program where I generate 20 random #'s 1-10 and place into an array. Then I generate 1 extra random number and search through the array and count how many times the extra random number occurs. I am stuck on the part where I need to count how many times the extra # occurs in the array. This is what I have so far...
using namespace std;
/******************************************************************************/
void randGen(int numArray[], int n); //function for random # generator
void randOne(int RanOne[], int m); //function to generate extra random # and display it
int main()
{
int RanNum[20]; //integer array to hold 20 numbers
randGen(RanNum, 20); //calls on function for random # generator and display #s
randOne (RanNum, 1); //calls on function to generate extra random # and display it
system ("pause"); //pauses system to view output
return 0;
} //end of main program
/******************************************************************************/
void randGen(int RanNum[], int n) //function for random # generator and display #s
{
srand(static_cast<unsigned int>(time(0))); //turns time into unsigned int
for(int RanNum=1;RanNum<=20;RanNum++) //generates 20 random numbers
{
int RanNum = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
cout << RanNum << endl; //displays numbers
}
} //end of function for random # generator and display #s
/******************************************************************************/
void randOne(int RanOne[], int m) //function to generate extra random # and display it
{
srand(static_cast<unsigned int>(time(0))); //turns time into unsigned int
{
int RanOne = rand() % 10 + 1; //generates random number 1-10
cout << endl << "Extra random # is ---> " << RanOne << endl <<endl; //display
}
} //end of function to generate extra random # and display it
/******************************************************************************/
void randGen(int RanNum[], int n) //function for random # generator and display #s
{
srand(static_cast<unsignedint>(time(0))); //turns time into unsigned int
for(int RanNum=1;RanNum<=20;RanNum++) //generates 20 random numbers
{
int RanNum = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
cout << RanNum << endl; //displays numbers
}
} //end of function for random # generator and display #s
you use the same name RanNum for three different variables hence you don't store the random number in your array. Plus: Seeding the random number generator is supposed to be done only once at the very beginning. So this srand(static_cast<unsignedint>(time(0))); //turns time into unsigned int is supposed to be in main() before everything else.
This:
1 2 3 4 5 6 7 8
void randGen(int RanNum[], int n) //function for random # generator and display #s
{
for(int i=0;i<20;i++) //generates 20 random numbers
{
RanNum[i] = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
cout << RanNum[i] << endl; //displays numbers
}
} //end of function for random # generator and display #s
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ctime>
usingnamespace std;
/******************************************************************************/
void randGen(int numArray[], int n); //function for random # generator
void randOne(int RanOne[], int m); //function to generate extra random # and display it
int counter = 0;
int main()
{
srand(static_cast<unsignedint>(time(0))); //turns time into unsigned int
int RanNum[20]; //integer array to hold 20 numbers
randGen(RanNum, 20); //calls on function for random # generator and display #s
randOne (RanNum, 1); //calls on function to generate extra random # and display it
system ("pause"); //pauses system to view output
return 0;
} //end of main program
/******************************************************************************/
void randGen(int RanNum[], int n) //function for random # generator and display #s
{
for(int i=0;i<20;i++) //generates 20 random numbers
{
RanNum[i] = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
cout << RanNum[i] << endl; //displays numbers
}
} //end of function for random # generator and display #s
/******************************************************************************/
void randOne(int RanOne[], int m) //function to generate extra random # and display it
{
{
int RanOne = rand() % 10 + 1; //generates random number 1-10
cout << endl << "Extra random # is ---> " << RanOne << endl <<endl; //display
}
} //end of function to generate extra random # and display it
/******************************************************************************/
Can anybody help with searching the array to display how many times the random # is generated or if it is not at all? And also setting up a Bubble Sort to put the #s in ascending order?
In randGen() use n instead of 20. That's the reason why n exists.
You don't need a function just to generate one random number.
Put line 46/47 before line 22. Rename randOne -> e.g. countRand(int RanNum[], int n, int Rand)
in "countRand" you can use a loop like in randGen. Instead of assigning a random number you need to compare (if(RanNum[i] == Rand)) and increase the count. At the end you cout count. count is a local variable set to 0 at the beginning.
Bubble Sort is similar to that above. You need two nested loops. You compare RanNum[i] > RanNum[j] -> swap value at position i with that at j
/************************** Compiler Directives ******************************/
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ctime>
usingnamespace std;
/*****************************************************************************/
void randGen(int numArray[], int n); //function for random # generator
void printRands(int numArray[], int n); //function to print out random #'s
void sortBubble(int numArray[], int n); //funtion to bubble sort array
int main()
{
int numArray[20];
int randNum;
int index;
int temp;
int counter; //counter to determine how many times random # occurs in array
char RunAgain; //used to store user's answer to run program again
do //beginning of While Loop to repeat the task
{
system ("cls"); //clears the system's screen
randGen(numArray, 20); //call on function to generate random numbers
printRands(numArray, 20); //call on function to display contents of array
sortBubble(numArray, 20); //call on function to bubble sort array
srand(static_cast<unsignedint>(time(0))); //turns time into unsigned int to get random numbers
randNum = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
cout<< "Extra random number is ---> " <<randNum<<endl; //print out random #
counter = 0; //counter to count how many times random # in array; set to 0
for (int index = 0; index < 20; index++)
{
if (randNum == numArray[index]) //if number occurs in array..
{
counter++; //then increase counter by 1
}
}
if (counter >0) //if number does not appear in array...
{
cout<<"The extra random number occurs "<<counter<<" time(s)."<<endl; //display
}
else //else...
{
cout<<"The extra random number did not occur at all."<<endl; //display
}
cout << endl << "Would you like to generate a new set of numbers? (Y/N): "; //display
cin >> RunAgain; //asks if user wants to re-run and store's the answer
} while (RunAgain == 'Y' || RunAgain == 'y'); //program will run again with Y/y
return 0;
} //end of main program
/******************************************************************************/
void randGen(int numArray[], int n) //function for random # generator and display #s
{
for(int i=0;i<20;i++) //generates 20 random numbers
{
numArray[i] = (rand() % (10 - 1 + 1)) + 1; //generates random number 1-10
}
} //end of function for random # generator and display #s
/******************************************************************************/
void printRands(int numArray[], int n) //function to display contents of array
{
cout<<"Your random numbers are: " << endl; //display
for(int i = 0; i < 20; i++) //array
{
numArray[i] = (rand() % (10 - 1 + 1)) + 1; //array
cout << setw(29) << numArray[i] << endl; //display array
}
} //end of function to display contents of array
/******************************************************************************/
int temp;
void sortBubble(int numArray[], int n)
{
for (int i = 0; i < (20-1);)
{
for (int j = 1; j< 20;)
{
if (numArray[j] < numArray[j-1])
{
numArray[j] = temp;
numArray[j-1] = numArray[j];
temp = numArray[j-1];
}
}
}
}
for (int i = 0; i < (20-1);) //No loop increment
{
for (int j = 1; j< 20;) //No loop increment and j can start with i +1
{
if (numArray[j] < numArray[j-1]) // compare array[i] and array[j]
{
numArray[j] = temp; // this will store garbage value
numArray[j-1] = numArray[j];
temp = numArray[j-1];
}
}
}
use array[i] and array[j] for swapping,use temp variable to store value from array
void sortBubbleAndPrint(int numArray[], int n)
{
int temp;
for (int i = 0; i < n -1 ; i++)
{
for (int j = i+1; j< n ; j++)
{
if (numArray[j] < numArray[i]) // changed for ascending order
{
temp = numArray[i];
numArray[i] = numArray[j];
numArray[j] = temp;
//if u put cout here this if may come upto (n-1)! times
}
}
}
// u can print it outside this function also
for (int i = 0; i < n ; i++)
{
cout<<numArray[i];
}
}
Dude, PLEASE, save all of us and yourself a lot of time and trouble and ismply use std::count from the header <algorithm>, mark it as solved, and everyone's happy, OK?
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <ctime>
usingnamespace std;
int* ranGenArr(int[]);
int extraRanNum(int&);
int* bubbleSort(int[]);
int extraNumMode(int[], int, int&);
void listFirst(int[], int);
void listSecond(int[], int);
int main()
{
srand(static_cast<unsignedint>(time(0)));
int ranArray[20];
int extraNumber = 0;
int counter = 0;
ranGenArr(ranArray);
extraRanNum(extraNumber);
listFirst(ranArray, extraNumber);
bubbleSort(ranArray);
extraNumMode(ranArray, extraNumber, counter);
listSecond(ranArray, counter);
system("PAUSE");
return 0;
}
int* ranGenArr(int ranArray[])
{
for(int i = 0; i < 20; i++)
{
ranArray[i] = (rand() % (10 - 1 + 1)) + 1;
}
return ranArray;
}
int extraRanNum(int& extraNumber)
{
extraNumber = rand() % 10 + 1;
return extraNumber;
}
int* bubbleSort(int ranArray[])
{
int newArray[20];
int m = 0;
for(int i = 1; i < 11; i++)
{
for(int n = 0; n < 20; n++)
{
if(i == ranArray[n])
{
newArray[m] = ranArray[n];
m++;
}
}
}
for(int i = 0; i < 20; i++)
{
ranArray[i] = newArray[i];
}
return ranArray;
}
int extraNumMode(int ranArray[], int extraNumber, int& counter)
{
for(int i = 0; i < 20; i++)
{
if(ranArray[i] == extraNumber)
{
counter++;
}
}
return counter;
}
void listFirst(int ranArray[], int extraNumber)
{
cout << "First Array Return: " << endl;
for(int i = 0; i < 20; i++)
{
cout << i + 1 << ". " << "[" << ranArray[i] << "]" << endl;
}
cout << "The extra number is: " << extraNumber << "." << endl;
}
void listSecond(int ranArray[], int counter)
{
cout << endl << "Second Array Return after Bubble Sort: " << endl;
for(int i = 0; i < 20; i++)
{
cout << i + 1 << ". " << "[" << ranArray[i] << "]" << endl;
}
cout << "The Extra Number Appears " << counter << " times." << endl;
}
First Array Return:
1. [6]
2. [3]
3. [4]
4. [7]
5. [7]
6. [2]
7. [6]
8. [4]
9. [7]
10. [1]
11. [2]
12. [5]
13. [3]
14. [2]
15. [5]
16. [2]
17. [4]
18. [8]
19. [8]
20. [3]
The extra number is: 4.
Second Array Return after Bubble Sort:
1. [1]
2. [2]
3. [2]
4. [2]
5. [2]
6. [3]
7. [3]
8. [3]
9. [4]
10. [4]
11. [4]
12. [5]
13. [5]
14. [6]
15. [6]
16. [7]
17. [7]
18. [7]
19. [8]
20. [8]
The Extra Number Appears 3 times.
Press any key to continue . . .
I worked for the past two days on this, as a personal challenge. This is what I came up with. Try it out and look at what I did/how I did it...I'm pretty sure I got the output you were looking for here. If you have any questions I would be happy to answer them for you, and if any more experienced c++ vet would like to look over this and give me feedback, I would appreciate it.
P.S. I know posting full source code and not just the part the OP asked for explanations on is looked down upon on this forum, but I ask the community to forgive me this time, as I will also be learning how arrays work by posting my full source here (as well as potentially helping the OP).