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

1. Write code that fills an array values with each set of numbers below using as

ID: 3775394 • Letter: 1

Question

1.   Write code that fills an array values with each set of numbers below using as few statements as possible (i.e., consider using a loop for each).

a.   1 2 3 4 5 6 7 8 9 10

b.   2 4 6 8 10 12 14 16 18 20

c.    1 4 9 16 25 36 49 64 81 100

d.   0 0 0 0 0 0 0 0 0 0

e.   1 4 9 16 9 7 4 9 11

f.     0 1 0 1 0 1 0 1 0 1

g.   0 1 2 3 4 0 1 2 3 4

2.   Consider the following array:

int[] a = { 1, 2, 3, 4, 5, 4, 3, 2, 1, 0 };


What is the value of total after each of the following loops complete?

a. int total = 0; for (int i = 0; i < 10; i++) { total = total + a[i]; }

b. int total = 0; for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; }

c. int total = 0; for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; }

d. int total = 0; for (int i = 2; i <= 10; i++) { total = total + a[i]; }

e. int total = 0; for (int i = 1; i < 10; i = 2 * i) { total = total + a[i]; }

f. int total = 0; for (int i = 9; i >= 0; i--) { total = total + a[i]; }

g. int total = 0; for (int i = 9; i >= 0; i = i - 2) { total = total + a[i]; }

h. int total = 0; for (int i = 0; i < 10; i++) { total = a[i] - total; }
  

Explanation / Answer

[2]

#include <stdio.h>
#include <conio.h>

int main() {
int i,total;
int a[10] = {1,2,3,4,5,4,3,2,1,0};
clrscr();

printf("Print the result of part A");

total = 0;
for(i=0;i<10;i++)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part B");

total = 0;
for(i=0;i<10;i=i+2)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part C");

total = 0;
for(i=1;i<10;i=i+2)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part D");

total = 0;
for(i=2;i<10;i++)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part E");

total = 0;
for(i=1;i<10;i=2*i)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part F");

total = 0;
for(i=9;i>0;i--)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part G");

total = 0;
for(i=9;i>0;i=i-2)
{
total=total + a[i];
}

printf(" : %d ",total);

printf("------------------------------------------------------");
printf(" Print the result of part H");

total = 0;
for(i=0;i<10;i++)
{
total= a[i] - total;
}

printf(" : %d ",total);


getch();
return 0;
}

--------------------------------------------------------------------------------------------------------------------------------

[1]

#include <stdio.h>
#include <conio.h>

int main() {
int array[20][20];
int i,j,a,b;
clrscr();


printf("enter the number of array ");
scanf("%d",&a);

for(i=0;i<a;i++)
{
printf(" enter the length of array ");
scanf("%d",&b);
printf(" enter the number ");
for(j=0;j<b;j++);
{
scanf(" %d ",&array[i][j]);
}
}

getch();
return 0;

}