2.) Create codes for the following questions on getting scores and amounts in C+
ID: 3589840 • Letter: 2
Question
2.) Create codes for the following questions on getting scores and amounts in C++ language.
A)
Write a function GetScores(...) that is given a string, an int type array to fill and the max number of elements in the array as parameters and returns an integer result. The function will find each substring of the given string separated by space characters and place it in the array. The function returns the number of integers extracted.
For example:
GetScores("123 456 789", int values[], int max_values )
would return the count of 3 and fill the array with integers 123, 456, 789.
B)
Write a function GetAmounts(...) that is given a string, an array to fill and the max number of elements in the array as parameters and returns an integer result. The function will separate each substring of the given string between space characters and place it in the array.. The function returns the number of values extracted.
For example:
GetAmounts("1.23 4.56 7.89", float values[], int max_values )
would return the count of 3 and fill the array with floating point values 1.23, 4.56, 7.89.
Explanation / Answer
Program - a:
#include <iostream>
/* this is used for importing string stream */
#include <sstream>
using namespace std;
/*
Function Name: GetScores;
Return Type : integer;
Input Parameters:
1.String str which is given by the user
2.Integer array named arr in which extracted integer are filled
3.Int max containing the value of maximum numbers that can be extracted
*/
int GetScores(string str, int arr[] , int max)
{
//creating a string stream object
stringstream string_stream;
//variable to keep track of number_of_integers found;
int number_of_elements = 1 ;
// Store the entire input string into string stream
string_stream << str;
//creating a string variable named str_temp
string str_temp;
// Running a loop till the string stream ends
//variable to keep track of the integer found
int integer_found;
while (!string_stream.eof() && number_of_elements <= max) {
// extracting word by word from stream
string_stream >> str_temp;
// checking if extracted word is an integer
if (stringstream(str_temp) >> integer_found){
//then place the integer in the array
arr[number_of_elements-1] = integer_found ;
// increase number of integer_found
number_of_elements++;
}
str_temp = "";
}
//return required number of integers found
return number_of_elements-1;
}
int main()
{
string str = "123 456 789";
//creating an array of size 100
int arr[100];
// sample calling the function for max = number_of_integers
cout << GetScores(str,arr,3) <<endl;
//calling the function for max > number_of_integers
cout << GetScores(str,arr,5) <<endl;
//calling the function for max < number_of_integers
cout << GetScores(str,arr,2) <<endl;
return 0;
}
Output:
3
3
2
Program -b :
#include <iostream>
/* this is used for importing string stream */
#include <sstream>
using namespace std;
/*
Function Name: GetScores;
Return Type : integer;
Input Parameters:
1.String str which is given by the user
2.Float array named arr in which extracted float values are filled
3.Int max containing the value of maximum numbers that can be extracted
*/
int GetScores(string str, float arr[] , int max)
{
//creating a string stream using input string str
stringstream string_stream(str);
//variable to keep track of number_of_float found;
int number_of_elements = 1;
while (!string_stream.eof() && number_of_elements <= max) {
//push the extracted float value into the array
string_stream >> arr[number_of_elements-1];
// increase number of float_found
number_of_elements++;
}
//return required number of float_values found
return number_of_elements-1;
}
int main()
{
string str = "35.2263 56.123 344.226 9.54223";
float arr[100];
// sample calling the function for max = number_of_integers
cout << GetScores(str,arr,4) <<endl;
//calling the function for max > number_of_integers
cout << GetScores(str,arr,5) <<endl;
//calling the function for max < number_of_integers
cout << GetScores(str,arr,2) <<endl;
return 0;
}
Output:
4
4
2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.