Arrays, loops, functions: Lotto
Element Repeated Function
Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index.
Generate Random Array
Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a loop that assigns a random value in the range [1..25] to each element of the array. When assigning an element, loop and regenerate the random number if the Element Repeated function determines that number already exists in the array.
Input Array
Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a loop that asks the user to input a number between 1 and 25 for each element of the array. Use an input validation while loop to ensure these numbers are within range and that the input did not fail. Clear out the read buffer if the input failed. Assign each input to an element of the array.
Show Array
Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a loop that outputs each element of the array.
Count Matches
Write a function that takes as parameters two arrays of integers and another integer for the size of the arrays. Loop through each element of the first array. With a nested loop, compare that element to each element of the second array. If a match is found, increment a counter. When the loops are done return the counter, which is the number of matches in any order.
Main
Declare two arrays and make them size 5. Ask the user if they would like to play the lottery. Loop as long as their answer is "y". Within the loop, call your Input Array function. Call the Generate Random Array function. Call the Show Array function for each array. Call the Count Matches function and output the count from its return value. If the number of matches is 5, tell them they won the jackpot. Then ask the user if they would like to play again and loop.
2D Arrays: Basic Processing
As done in lecture, write functions to perform the following on a 2D array: Fill each cell with a random number, print each cell with a newline for each row, return the average, return the sum of a row, and return the sum of a column.
C++ Strings, Streams: Parse a file that uses commas to separate columns.
int getColumn (string line, int start, string& column)
Implement the above function to loop through each character of line, starting at index start, and assign to column a string containing each character up to but not including the next comma ',', then return the index of where the comma was found. You may not use any string library functions other than .size(). Declare an output string as empty and concatenate (+=) each character into it as you go.
Main
Create an output file called "out.txt". Assume there exists a file called "Data.txt" that contains three fields per record, separated by commas, with a newline separating each record. For example, the first row contains "Hermle, Ryan, $3.50\n". Open the input file, check that it opened properly, and loop to read one line at a time. Within the read loop, call getColumn three times, adjusting the start index each time, to extract the three columns, not including the commas, into three separate string variables. For each line, output them in the format of "Gregory|Chapman|$3.50\n" to "out.txt".
1st code
#include<cstdlib>
#include<iostream>
using namespace std;
bool func1(int arr[],int el,int st,int ed){
for(int i=0;i<ed-1;i++){
if(el==arr[i]) return true;
}
return false;
}
void func2(int arr[],int n){
for(int i=0;i<n;i++){
int rn;
do{
rn=rand()%25+1;
}
while(func1(arr,rn,0,i+1));
arr[i]=rn;
}
}
void func3(int arr[],int n){
for(int i=0;i<n;i++){
int rn;
do{
cout<<"Enter a number";
cin>>rn;
}
while(rn>25 && rn<1);
arr[i]=rn;
}
}
void func4(int arr[],int n){
for(int i=0;i<n;i++) cout<<arr[i]<<" ";
cout<<endl;
}
int func5(int arr1[],int arr2[],int n){
int c=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(arr1[i]==arr2[j]) c++;
}
}
return c;
}
int main(){
int n=5;
int arr1[n],arr2[n];
char askans;
do{
cout<<"Do you want to play the lottery? y/n"<<endl;
cin>>askans;
if(askans=='n'){
break;
}
func3(arr1,n);
func3(arr2,n);
func2(arr1,n);
func2(arr2,n);
func4(arr1,n);
func4(arr2,n);
int cnt=func5(arr1,arr2,n);
cout<<"No of matches: "<<func5(arr1,arr2,n)<<endl;
if(cnt==5) cout<<"You won the jackpot"<<endl;
}while(askans=='y');
return 0;
}
2)
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int getColumn(string line,int start,string &column){
string temp="";
int j=0;
for(int i=start;i<line.size();i++){
if(line[i]==','){
column+=temp;
column+='|';
temp="";
}
else
temp+=line[i];
}
column+=temp;
}
int main(){
ofstream readfile;
readfile.open("data.txt");
string myText;
while (getline (readfile, myText)) {
string column="";
getColumn(myText,0,column);
cout<<column<<endl;
}
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.