C++ Answers

Questions answered by Experts: 9 913

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search

Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}.


#include <iostream>

using namespace std;


/* Your solution goes here */


int main() {

  const int SORT_ARR_SIZE = 4;

  int sortArray[SORT_ARR_SIZE];

  int i;

  int userNum;


  for (i = 0; i < SORT_ARR_SIZE; ++i) {

   cin >> userNum;

   sortArray[i] = userNum;

  }


  SwapArrayEnds(sortArray, SORT_ARR_SIZE);


  for (i = 0; i < SORT_ARR_SIZE; ++i) {

   cout << sortArray[i] << " ";

  }

  cout << endl;


  return 0;

}


Type the output for the given program when the input is

1

7

10

9

6


#include <iostream>

using namespace std;


int FindScore(const int scoreVals[], int numVals) {

int i;

int returnVal = scoreVals[0];


for (i = 1; i < numVals; ++i) {

if (scoreVals[i] < returnVal) {

returnVal = scoreVals[i];

}

}


return returnVal;

}


int main() {

const int NUM_SCORES = 5;

int quizScores[NUM_SCORES];

int i;

int returnScore;


for (i = 0; i < NUM_SCORES; ++i) {

cin >> quizScores[i];

}


returnScore = FindScore(quizScores, NUM_SCORES);

cout << returnScore << endl;


return 0;

}


Double any element's value that is less than controlValue. Ex: If controlValue = 10, then dataPoints = {2, 12, 9, 20} becomes {4, 12, 18, 20}.

#include <iostream>

using namespace std;


int main() {

  const int NUM_POINTS = 4;

  int dataPoints[NUM_POINTS];

  int controlValue;

  int i;


  cin >> controlValue;


  for (i = 0; i < NUM_POINTS; ++i) {

   cin >> dataPoints[i];

  }


  /* Your solution goes here */


  for (i = 0; i < NUM_POINTS; ++i) {

   cout << dataPoints[i] << " " ;

  }

  cout << endl;


  return 0;

}


Write a loop that sets each array element to the sum of itself and the next element, except for the last element which stays the same. Be careful not to index beyond the last element. Ex:

Initial scores:        10, 20, 30, 40
Scores after the loop: 30, 50, 70, 40

The first element is 30 or 10 + 20, the second element is 50 or 20 + 30, and the third element is 70 or 30 + 40. The last element remains the same.

#include <iostream>

using namespace std;


int main() {

  const int SCORES_SIZE = 4;

  int bonusScores[SCORES_SIZE];

  int i;


  for (i = 0; i < SCORES_SIZE; ++i) {

   cin >> bonusScores[i];

  }


  /* Your solution goes here */


  for (i = 0; i < SCORES_SIZE; ++i) {

   cout << bonusScores[i] << " ";

  }

  cout << endl;


  return 0;

}


Write a loop that sets newScores to oldScores shifted once left, with element 0 copied to the end. Ex: If oldScores = {10, 20, 30, 40}, then newScores = {20, 30, 40, 10}.


Note: These activities may test code with different test values. This activity will perform two tests, both with a 4-element array (int oldScores[4]). See "How to Use zyBooks".


If the submitted code tries to access an invalid array element, such as newScores[9] for a 4-element array, the test may generate strange results. Or the test may crash and report "Program end never reached".


#include <iostream>

using namespace std;

int main() {

  const int SCORES_SIZE = 4;

  int oldScores[SCORES_SIZE];

  int newScores[SCORES_SIZE];

  int i;


  for (i = 0; i < SCORES_SIZE; ++i) {

   cin >> oldScores[i];

  }

  /* Your solution goes here */

  for (i = 0; i < SCORES_SIZE; ++i) {

   cout << newScores[i] << " ";

  }

  cout << endl;

  return 0;

}


Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}.


int main() {

  const int SCORES_SIZE = 4;

  int lowerScores[SCORES_SIZE];

  int i;


  for (i = 0; i < SCORES_SIZE; ++i) {

   cin >> lowerScores[i];

  }


  /* Your solution goes here */


  for (i = 0; i < SCORES_SIZE; ++i) {

   cout << lowerScores[i] << " ";

  }

  cout << endl;


  return 0;

}


Multiply each element in origList with the corresponding value in offsetAmount. Print each product followed by a semicolon (no spaces).


Ex: If origList = {4, 5, 10, 12} and offsetAmount = {2, 4, 7, 3}, print:

8;20;70;36;


#include <iostream>

#include <string.h>

using namespace std;


int main() {

  const int NUM_VALS = 4;

  int origList[NUM_VALS];

  int offsetAmount[NUM_VALS];

  int i;


  cin >> origList[0];

  cin >> origList[1];

  cin >> origList[2];

  cin >> origList[3];


  cin >> offsetAmount[0];

  cin >> offsetAmount[1];

  cin >> offsetAmount[2];

  cin >> offsetAmount[3];


  /* Your code goes here */


  cout << endl;


  return 0;

}


Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp = {90, 92, 94, 95}, print:

90, 92, 94, 95

Your code's output should end with the last element, without a subsequent comma, space, or newline.


#include <iostream>

using namespace std;


int main() {

  const int NUM_VALS = 4;

  int hourlyTemp[NUM_VALS];

  int i;


  for (i = 0; i < NUM_VALS; ++i) {

   cin >> hourlyTemp[i];

  }


  /* Your solution goes here */


  cout << endl;


  return 0;

}


Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8.


#include <iostream>

using namespace std;


int main() {

  const int NUM_VALS = 4;

  int testGrades[NUM_VALS];

  int i;

  int sumExtra = -9999; // Assign sumExtra with 0 before your for loop


  for (i = 0; i < NUM_VALS; ++i) {

   cin >> testGrades[i];

  }


  /* Your solution goes here */


  cout << "sumExtra: " << sumExtra << endl;

  return 0;

}


Write a for loop to populate array userGuesses with NUM_GUESSES integers. Read integers using cin. Ex: If NUM_GUESSES is 3 and user enters 9 5 2, then userGuesses is {9, 5, 2}.


#include <iostream>

using namespace std;


int main() {

  const int NUM_GUESSES = 3;

  int userGuesses[NUM_GUESSES];

  int i;


  /* Your solution goes here */


  for (i = 0; i < NUM_GUESSES; ++i) {

   cout << userGuesses[i] << " ";

  }


  return 0;

}


LATEST TUTORIALS
APPROVED BY CLIENTS