Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Populate a one-dimensional array with the following grades in this order:

90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, and 66. 

Use a loop to call a method from main() that adds 15 points to all student grades that are a C or above.  A C is defined as a score of 70 or above.  Make this happen by passing the subscript value and not the entire array. The addition of the 15 points should happen in the method and be passed back to main.  Use a loop to show final values within the array.  The final array values should show the new adjusted grades.


What I have tried:

#include <iostream>
using namespace std;
void func(int& num)
{
	if (num >= 70)
		num += 15;
}
int main()
{
	int arr[] = { 90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, 66 };// array number 
	for (int i = 0; i < 14; i++)
	{
		func(arr[i]);
	}
	cout << " The new adjusted grade scores are: ";
	for (int i = 0; i < 14; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
	return 0;
}
Posted
Updated 11-Nov-20 6:57am
Comments
Joe Woodbury 10-Nov-20 18:25pm    
Is the assumption that grades are capped at 100?
Japanese Relaxation 10-Nov-20 18:55pm    
yes
Dave Kreskowiak 10-Nov-20 19:37pm    
OK, so what code do you have to make sure the score doesn't go over 100?
Japanese Relaxation 10-Nov-20 19:53pm    
yeah not over 100
jeron1 10-Nov-20 20:12pm    
what happens when the starting score is 98?

I am not sure why you would be constrained like that but it appears that you must pass only the subscript which means the data must be globally accessible. As mentioned previously, the value fourteen should NOT be hard-coded. Here is one way you can deal with these things :

C++
#include <iostream>

const int ScoreCount = 14;
int Scores[ ScoreCount ] = { 90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, 66 };

void func( int index )
{
    if( Scores[ index ] >= 70 )
        Scores[ index ] += 15;
}

int main()
{
    for( int i = 0; i < ScoreCount; i++ )
    {
        func( i );
    }

    std::cout << " The new adjusted grade scores are: ";

    for( int i = 0; i < ScoreCount; i++ )
    {
        std::cout << i << " : " << Scores[ i ] << std::endl;
    }
    return 0;
}
 
Share this answer
 
Comments
KarstenK 11-Nov-20 12:52pm    
Not at all: it can be solved via pointers or ref syntax :-O
Rick York 11-Nov-20 18:09pm    
I was interpreting the phrase "subscript value" differently. To me that meant the index into the array - the value of the subscript. If you interpret it mean the value at the index then it is considerably easier. Vague wording always annoys me.
only add this, which assign the result to the input:
C++
for (int i = 0; i < 14; i++)
{
	arr[i] = func(arr[i]);
}
You better dont write fancy stuff which you cant explain to the teacher or in front of your class.

Tip visit some Learn C++ tutorial to learn the language and state of the art tools.
 
Share this answer
 
Comments
CPallini 11-Nov-20 15:49pm    
5.
You're using a hard coded 14 in your for loop. Not exactly "wrong", but will fail any time you change the size of the array. You should use the length of the array to limit the for loop.
 
Share this answer
 
Comments
Japanese Relaxation 10-Nov-20 21:13pm    
Would you mind may you show me ?
Yet another way to do that
C++
#include <iostream>
using namespace std;

int main()
{
  int arr[] = { 90, 61, 74, 42, 83, 51, 71, 83, 98, 87, 94, 68, 44, 66 };

  auto transform = [& arr](int index){ arr[index] += (arr[index] > 70 ? 15 : 0); };


  for (size_t i=0; i<sizeof(arr)/sizeof(arr[0]); ++i)
    transform(i);


  for (auto a : arr)
    cout << a << ", ";
  cout << endl;

}
 
Share this answer
 
Comments
KarstenK 11-Nov-20 12:53pm    
This solution shouldnt be ONLY from someone who can "explain" it to his teacher ;-)
CPallini 11-Nov-20 15:48pm    
Teachers badly need someone who encourages them to teach modern C++.
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900