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 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...
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...
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...
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...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
write a function call character_thing (string, character) that takes in a string and a character. The...
write a function call character_thing (string, character) that takes in a string and a character. The function will return the number of times the character appears in the string. please be in python
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...