Looping Designed to test skills: repetition structures (for and while loops), ap
ID: 1920907 • Letter: L
Question
Looping
Designed to test skills: repetition structures (for and while loops), application of repetition structures (user input, accumulation, aggregation, etc.), incrementing the loop index, and conditional statements
Additional guidelines: complete and submit this first page with your other responses
1) The code below serves to demonstrate that a for loop functions like a while loop under specific conditions.
fprintf('Loop to show how a for loop operates ')
fprintf('as a specific type of while loop ')
fprintf('While loop ')
i = 1;
while i <= 10
disp (i)
i = i + 2;
end
fprintf('For loop ')
for i = 1:2:10;
disp(i)
end
Mark the following in both loops using the guidelines given below.
a. Circle the variable used as the loop indexer
b. Draw a square around the initial value of the index
c. Draw a triangle around the final value of the index
d. Underline the number used to increment the value of the index between loops
2) Write the word
Explanation / Answer
1)
A) Loop Indexer is variable "i";
B) for "whille" loop initial value of index is line 4 i.e, "i = 1;".
and for "for" loop initial value of index is in line 10 "for i = 1:2:10", so i=1 is initial value of loop indexer.
C) for "whille" loop final value of index is "i = i + 2;" and after loop execution i will become 11;
and for "for" loop final value of index is in line 10 "for i = 1:2:10", and after loop execution i will become 11;
D) for "whille" loop number used to increment the value of the index between loops is 2 in line "i = i + 2;"
and for "for" loop number used to increment the value of the index between loops is "for i = 1:2:10" in line 10 and after loop execution i will become 11;
2)
A) for
B) for
C) while
D) for
3)
A) while loop inside a for loop
B) for loop inside a while loop
C) for loop inside a for loop
4)
#include<stdio.h>
int main()
{
int i=0;
int x[3],y[3],z[3];
char a[3][50];
while(i<3)
{
printf("Enter student name ");
scanf("%s",a[i]);
printf("Enter marks of quiz 1 ");
scanf("%d",&x[i]);
printf("Enter marks of quiz 2 ");
scanf("%d",&y[i]);
printf("Enter marks of quiz 3 ");
scanf("%d",&z[i]);
i++;
}
int j=0;
while(j<i)
{
printf("Student Name%s ",a[j]);
printf("marks%d ",x[j]+y[j]+z[j]);
j++;
}
return 0;
}
This code will ask for student name then his/her marks in all three quizes for 3 times.
and finally print student name followed by his/her total marks for all 3 students.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.