Question

Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...

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".

Homework Answers

Answer #1

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;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a c++ function which takes two parameters: an array of ints and an int size...
Write a c++ function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void printSome(const int array[], int size);
In C programming language write a function that takes two arrays as input m and n...
In C programming language write a function that takes two arrays as input m and n as well as their sizes: size_m and size_n, respectively. Then it checks for each element in m, whether it exists in n. The function should update a third array c such that for each element in m: the corresponding position/index in c should be either 1, if this element exists in m, or 0, if the element does not exist in n
Write a function that takes in 3 arguments: a sorted array, size of the array, and...
Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question.
Write a function called matches that takes two int arrays and their respective sizes, and returns...
Write a function called matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ Im confused how to start this, any hints are great and...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a copy of the array, except that the element values should be reversed in the copy. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array,...
C++ Write a function that takes in 3 arguments: a sorted array, size of the array, and an integer number. It should return the position where the integer value is found. In case the number does not exist in that array it should return the index where it should have been if it were present in this sorted array. Use pointer notation of arrays for this question. c++ code
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns...
Write a function in c using #include <stdio.h> that takes a one-dimensional integer array and returns the index of the first occurance of the smallest value in the array. Your function must be able to process all the elements in the array. Create a function prototype and function definition (after the main function). Your main function should declare a 100 element integer array. Prompt the user for the number of integers to enter and then prompt the user for each...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs...
Task 2: Compare strings. Write a function compare_strings() that takes pointers to two strings as inputs and compares the character by character. If the two strings are exactly same it returns 0, otherwise it returns the difference between the first two dissimilar characters. You are not allowed to use built-in functions (other than strlen()) for this task. The function prototype is given below: int compare_strings(char * str1, char * str2); Task 3: Test if a string is subset of another...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
Write a function that accepts an int array and the array’s size as arguments. The function...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array. Demonstrate the function by using it in a main program that reads an integer N...