Question

Write a function getScores(...) that is given a string, an int type array to fill and...

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:

int values[3];
getScores("123 456 789", values, 3 ); 

would return the count of 3 and fill the array with integers 123, 456, 789.


Note: The write-up explains a built-in function available which converts a string into an integer.

C++ Only Please.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

//including the string stream library to convert the string to number

#include <sstream>

using namespace std;

//function to extract and convert the numbers from text

//and store them in array

void getScores(string text,int values[],int count)

{

                //finding the length of the text

                int len=text.length();

                //declaring required variables

                //i to store current pointing index in text

                //index to store the index of the array

                int i=0,index=0;

                //number is the string to store each number in string format

                string number="";

               

                //loop to extract and convert the text to numbers

                //and add the numbers to array

                while(i<=len)

                {

                                //if the individual number is completed

                                if(text[i]==' ' || i==len)

                                {

                                                //creating the stream of the string and converting into number

                                                stringstream geek(number);

                                                //storing the int format of string to array

                                                geek>>values[index];

                                                //updating the pointer in array

                                                index++;

                                                //upadting the number variable

                                                number="";

                                                //end

                                }

                                //if the current character is a digit

                                else

                                {

                                                //updating the number variable

                                                number=number+text[i];

                                                //end

                                }

                                //updating the pointer of the text

                                i++;

                                //eend of loop

                }

                //end of function

}

//main function to test the function

int main()

{

                //sample run 1

                //creating the creating the variables

                int values[3],i;

                //calling the function

                getScores("123 456 789", values, 3 );

                //printing the header

                cout<<"getScores(\"123 456 789\", values, 3 )        :: ";

                //printing the int array

                for(i=0;i<3;i++) cout<<values[i]<<",";

                //end of sample run 1

               

               

                //sample run 2

                //creating the creating the variables

                int values_2[5];

                //calling the function

                getScores("98 76 54 123 1", values_2, 5 );

                //printing the header

                cout<<"\ngetScores(\"98 76 54 123 1\", values_2, 5 ) :: ";

                //printing the int array

                for(i=0;i<5;i++) cout<<values_2[i]<<",";

                //end of sample run 2

               

                //end of main function

}

CPU Time 0.00 sec(s), Memory: 3244 kilobyte(s) getscores (123 456 789, values, 3 ) getScores (98 76 54 123 1, values_2, 5) : 123,456,789, 98,76,54,123,1,

Add a comment
Know the answer?
Add Answer to:
Write a function getScores(...) that is given a string, an int type array to fill and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int)

    please do in c++Q4. Using recursion only (no loops allowed!), write a C++ function of the form int count7s(int) that when passed a non-negative integer, returns the number of occurrences of the digit 7 in the number. So, for example: count7s(717) → 2 count75(7) →1 count7s(123) → 0 Q5. Write a C++ function of the form string mirrorEnds(string)that when given a string, looks for a mirror image (backwards) string at both the beginning and end of the given string. In other words, zero or...

  • 1. Write a complete program based on public static int lastIndexOf (int[] array, int value) {...

    1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...

  • 6. As a practical joke, a friend gives you an int array for your birthday. As...

    6. As a practical joke, a friend gives you an int array for your birthday. As if that weren’t bad enough, your friend tells you that the array contains almost all 0’s except for a small string of consecutive 1’s contained somewhere in the middle. Overwhelmed by the novelty of this you decide to write a function that will print out the location of the first 1 in the array, the location of the last 1 in the array, and...

  • 1. Write a function named findTarget that takes three parameters: numbers, an array of integers -...

    1. Write a function named findTarget that takes three parameters: numbers, an array of integers - size, an integer representing the size of array target, a number The function returns true if the target is inside array and false otherwise 2. Write a function minValue that takes two parameters: myArray, an array of doubles size, an integer representing the size of array The function returns the smallest value in the array 3. Write a function fillAndFind that takes two parameters:...

  • JavaScript 1. Write a function which receives one argument (string) and converts the string to uppercase....

    JavaScript 1. Write a function which receives one argument (string) and converts the string to uppercase. function('abc') // returns 'ABC' 2. Write a function that accepts an array and a value. Find and return the first array element with bigger than given value. function([5,15,4],6) //returns 15 DO NOT USE THESE IN-BUILT FUNCTIONS BELOW endsWith() includes() indexOf() lastIndexOf() localeCompare() match() repeat() replace() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() trim() valueOf() trimLeft and trimRight unshift() valueOf() includes()...

  • a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string>...

    a. Write a function arrayToMap that takes an array of strings and returns a std::map<int, string> such that the values in the map are the string values in the array of strings, and the keys in the map are the corresponding array indices of the string values. You may assume all necessary libraries have been included in your program and your solution must be syntactically correct in order to receive full credit. map<int, string> arrayToMap(string arr[], int arrSize) { b....

  • Do the following: - Write and test a function that takes an array of doubles and...

    Do the following: - Write and test a function that takes an array of doubles and returns the average of the values in the array - Write and test a function that takes an array of doubles and returns number of values in the array that are above the average of the values in the array - Write and test a function that takes an array of Strings and returns number of values in the array that start with an...

  • Given an array and a starting position write a function replaceFromN, that takes an integer array...

    Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing. void replaceFromN(int array[], int size, int n) For example, given array= {15,12,4,9,2,3} n =2 the function should modify array to be {15,12,1,2,3,4}

  • Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...

    Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. To test your function, write a main that prompts a user for a list of 15 integers and outputs the index and value of the first occurrence of the smallest value. The program should print out Enter 15 integers: The position of the first occurrence of the smallest element in...

  • help me solving this both please in c++ language 6 Write function max(int al Lint n)...

    help me solving this both please in c++ language 6 Write function max(int al Lint n) that receives an array of integer and its length, it returns the maximum number in that array. Test your function in main. 7) Write functin reverse (char x[ .int n) that reieves an array of characters and reverse the order of its elements. Test this function in main.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT