I try to get close to the solution for question one but still getting errors in
ID: 3791053 • Letter: I
Question
I try to get close to the solution for question one but still getting errors in visual studio, and question 2 I do not know how to set it up. Need help for both of them?
1.Execute the following coding segment and identify the errors in the program. Debug the program and provide the correct version of the code. Note: The errors can be syntactical or logical.
2. Providing the correct code from up above adjust the program to hold 10 elements in the array and store the value of 100 in each. Submit the new code for review.
#includeExplanation / Answer
Solution for a):-
In given Solution:-
#include<stdio.h>
int main()
{
int value[3]={0,0,0}; //array declaration
int i;
for(i=0;i<5;i++) // step through each element
//In above line their is a error the value array size is 3 but it repeats for 5.
{
value[i]=1; //assign each element the value.
//when i value become 3 it gives an error.
printf(" Value[%d] = %d ",i,value[i]);
}
printf(" ");
return 0; //terminate sigment.
}
Correct Solution :-
#include<stdio.h>
int main()
{
int value[5]={0,0,0,0,0}; //array declaration
int i;
for(i=0;i<5;i++) // step through each element
{
value[i]=1; //assign each element the value.
printf(" Value[%d] = %d ",i,value[i]);
}
printf(" ");
return 0; //terminate program.
}
Output :-
Value[0] = 1
Value[1] = 1
Value[2] = 1
Value[3] = 1
Value[4] = 1
Solution for b)
Program :-
#include<stdio.h>
int main()
{
int value[10]; //array declaration
int i;
for(i=0;i<10;i++) // step through each element
{
value[i]=100; //assign each element the value.
printf(" Value[%d] = %d ",i,value[i]);
}
printf(" ");
return 0; //terminate program.
}
Output :-
Value[0] = 100
Value[1] = 100
Value[2] = 100
Value[3] = 100
Value[4] = 100
Value[5] = 100
Value[6] = 100
Value[7] = 100
Value[8] = 100
Value[9] = 100
Thank you!