Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Test Review C Programming 1-) Write a loop that subtracts 1 from each element in

ID: 673129 • Letter: T

Question

Test Review C Programming

1-) 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}.

#include <stdio.h>

int main(void) {
const int SCORES_SIZE = 4;
int lowerScores[SCORES_SIZE];
int i = 0;

lowerScores[0] = 5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;

  
for(i=0;i<SCORES_SIZE;++i){
if(lowerScores[i]-1 =<0);
lowerScores[i]=0;
else
lowerScores[i] =lowerScores[i] - 1;
}
/* Your solution goes here */

for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", lowerScores[i]);
}
printf(" ");

return 0;
}

2-)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}.

#include <stdio.h>

int main(void) {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0;

oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;

/* Your solution goes here */

for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", newScores[i]);
}
printf(" ");

return 0;
}

3-)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:

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 <stdio.h>

int main(void) {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;

bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;

/* Your solution goes here */

for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", bonusScores[i]);
}
printf(" ");

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

#include <stdio.h>

int main(void) {
const int NUM_POINTS = 4;
int dataPoints[NUM_POINTS];
int minVal = 0;
int i = 0;

dataPoints[0] = 2;
dataPoints[1] = 12;
dataPoints[2] = 9;
dataPoints[3] = 20;

minVal = 10;

/* Your solution goes here */

for (i = 0; i < NUM_POINTS; ++i) {
printf("%d ", dataPoints[i]);
}
printf(" ");

return 0;
}

return 0;
}

Explanation / Answer

1-) 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}.

#include<stdio.h>
int main(void)
{
const int SCORES_SIZE = 4;
int lowerScores[SCORES_SIZE];
int i = 0;
lowerScores[0] = -5;
lowerScores[1] = 0;
lowerScores[2] = 2;
lowerScores[3] = -3;
  
for(i=0;i<SCORES_SIZE;++i){
if(lowerScores[i]-1 <=0)
lowerScores[i]=0;
else
lowerScores[i] =lowerScores[i] - 1;
}
/* Your solution goes here */
for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", lowerScores[i]);
}
printf(" ");
return 0;
}

output : {4, 0, 1, 0}
2-)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}.

#include <stdio.h>
int main(void) {
const int SCORES_SIZE = 4;
int oldScores[SCORES_SIZE];
int newScores[SCORES_SIZE];
int i = 0,j=1;
oldScores[0] = 10;
oldScores[1] = 20;
oldScores[2] = 30;
oldScores[3] = 40;

  
for (j = 1; j < SCORES_SIZE; ++j,i++) {
  
newScores[i]=oldScores[j];
  

}
  
newScores[SCORES_SIZE-1]=oldScores[0];
  
for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", newScores[i]);
}
printf(" ");
return 0;
}


output : {20, 30, 40, 10}

3-)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 <stdio.h>
int main(void) {
const int SCORES_SIZE = 4;
int bonusScores[SCORES_SIZE];
int i = 0;
bonusScores[0] = 10;
bonusScores[1] = 20;
bonusScores[2] = 30;
bonusScores[3] = 40;
/* Your solution goes here */
  
for (i = 0; i < SCORES_SIZE-1; ++i) {
bonusScores[i]+=bonusScores[i+1];
}
  
  
for (i = 0; i < SCORES_SIZE; ++i) {
printf("%d ", bonusScores[i]);
}
printf(" ");
}

Output : 30, 50, 70, 40

4-)Double any element's value that is less than minVal. Ex: If minVal = 10, then dataPoints
= {2, 12, 9, 20} becomes {4, 12, 18, 20}.
#include <stdio.h>
int main(void) {
const int NUM_POINTS = 4;
int dataPoints[NUM_POINTS];
int minVal = 10;
int i = 0;
dataPoints[0] = 2;
dataPoints[1] = 12;
dataPoints[2] = 9;
dataPoints[3] = 20;
minVal = 10;
/* Your solution goes here */
for (i = 0; i < NUM_POINTS; ++i) {
  
if(minVal>dataPoints[i]){
dataPoints[i]=2*dataPoints[i];
}
}
  
for (i = 0; i < NUM_POINTS; ++i) {
printf("%d ", dataPoints[i]);
}
printf(" ");
return 0;

}

Output : {4, 12, 18, 20}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote