In: Computer Science
A programmer that you work with, Peter, is a jerk. He is responsible for an array that is a key part of an important program and he maintains a sum of the array value.He won't give you access to this array; however, your boss has told you that you need to get input from the user and then place it into the array.Each evening Peter will scan the code and remove any illegal references to his array.
Using pointers, access Peter's array without him knowing it and place three values that you got from the user ONLY AT POSITIONS 3, 6, and 9. Recalculate the sum value and update it.
STARTER CODE BELOW:
#include <stdio.h>
#include
<stdlib.h>
int main(){
int petersArray[10] = {100,200,300,400,500,600,700,800,900,1000}; i
int petersArraySum = 0;
printf("Peter's Array:\n");
for (int loop = 0; loop < 10; loop++) {
printf("%d ",petersArray[loop]);
petersArraySum += petersArray[loop];
}
printf("\n");
printf("Peter's Array Sum: %d\n\n",petersArraySum);
return 0;
Code.
#include <stdio.h>
#include<stdlib.h>
void printArrayandGetSum(int a[])
{
int petersArraySum=0;
printf("Peter's Array:\n");
for (int loop = 0; loop < 10; loop++) {
printf("%d ",a[loop]);
petersArraySum += a[loop];
}
printf("\n");
printf("Peter's Array Sum: %d\n",petersArraySum);
}
int main(){
int petersArray[10] = {100,200,300,400,500,600,700,800,900,1000};
int petersArraySum = 0;
printArrayandGetSum(petersArray);
printf("\n");
int n1,n2,n3;
printf("Enter 3 numbers to be placed: ");
scanf("%d%d%d", &n1,&n2,&n3);
*(petersArray+2)=n1;
*(petersArray+5)=n2;
*(petersArray+8)=n3;
printArrayandGetSum(petersArray);
return 0;
}
Output:
Please
comment in case of doubts or queries.
It would be really helpful if you could upvote
:)