q : explain the code for a beginner in c what each line do
Question 2. The following code defines an array size that sums elements of the defined array through the loop.
#include <stdio.h>
#define A 10
int main(int argc, char** argv) {
int Total = 0;
int numbers[A];
for (int i=0; i < A; i++)
numbers[i] = i+1;
int i =0;
while (i<=A){
Total += numbers[i];
i++;
}
printf("Total =%d\n", Total);
return 0;
}
Code:
#include <stdio.h>
// we are defining constant value that can't changed
// the value of the A is 10 through the whole program
#define A 10
// define a main funcion
int main(int argc, char** argv) {
// this is integer variable Total = 0
int Total = 0;
// declare a array of size 10 since the value of A = 10
int numbers[A];
// using for loop iterate it 10 times
for (int i=0; i < A; i++)
// store the values inside the array
numbers[i] = i+1;
// end of for
// initialize the i value to 0
int i =0;
// using while loop iterate the while loop 10 times
while (i<A){
// sum the array elements
Total += numbers[i];
// increment the i value everytime
i++;
}
// print the result finally.
printf("Total = %d\n", Total);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.