Language C 1. Please save the program with the name ‘triangle.c’ Write a nested
ID: 3912968 • Letter: L
Question
Language C
1. Please save the program with the name ‘triangle.c’
Write a nested for loop that prints the following output:
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Hint: Here is the psudocode solution:
for row from 0 to 7 {
pad leading blanks in a row like this
for the column from 1 to 7-row:
print a set of spaces
print left half of row
for column from 0 to row:
print spaces followed by 2 raised to the power of the column
print right half of row
for column from row-1 to 0:
print spaces followed by 2 raised to the power of the column
print a new line
You need to figure out how many spaces to print before the number. The simple way to
compute powers of 2 is with the pow function; can you do this without it?
2. Please save the program with the name ‘grade.c’
Write a program that will prompt the user to input ten student scores to an array. The score
value is between 0 – 100. The program will display the smallest and greatest of those values. It
also displays the average score.
Example:
Explanation / Answer
Hello there!!!
I have wrote working codes for both problems
###########################################################
[1]
#include<stdio.h>
int power(int b,int d) {
if(d==0)
return 1;
else
return b*(power(b,d-1));
}
int main()
{
int i,j,k;
for(i=0;i<8;i++)
{
for (j=0;j<i;j++)
{
k = power(2,j);
printf("%d ",k);
}
for(j=i;j>=0;j--)
{
k = power(2,j);
printf("%d ",k);
}
printf(" ");
}
return 0;
}
#include<stdio.h>
#include<limits.h>
int main(){
int scores[10];
int min=100,max=0,sum=0;
float avg;
fprintf(stdout,"Please input student scores one at a time. ");
for(int i=0;i<10;i++) {
again:
fprintf(stdout,"Student %d : ",(i+1));
fscanf(stdin,"%d",&scores[i]);
if(scores[i]<0 || scores[i]>100) {
fprintf(stdout,"Try again! ");
goto again;
}
if(scores[i]>max)
max=scores[i];
if(scores[i]<min)
min=scores[i];
sum+=scores[i];
}
fprintf(stdout,"*****************Final Result**************** ");
fprintf(stdout,"The maximum score is %d ",max);
fprintf(stdout,"The minimum score is %d ",min);
fprintf(stdout,"The average score is %f ",(float)(sum/10));
return 0;
}
###############################################################
Thank you!!!
Feel free to ask queries and modifications!!!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.