1. Write a program that computes the volume of a box. The function main in the p
ID: 3535979 • Letter: 1
Question
1. Write a program that computes the volume of a box. The function main in the program calls a function to read the length, width and height of the box from the keyboard. The program then uses another function to compute the volume of the box:
(volume = length * width * height). The computed volume is to be printed from the function main. No global variables are allowed (have to use parameters in the functions).
2.What would be printed from the following program segment:
for (int x = 10 ; x > 7 ; x--)
printf ( "%d ", x ) ;Answer
b. Rewrite the same program using a do-while loop to compute and print the sum and average of the integers . Answer
Explanation / Answer
1. Voluem of Box
/*
* File: sum.c
* Author: Krishna
*
* Created on 11 May, 2013, 8:30 AM
*/
#include <stdio.h>
/*
*
*/
float calculateVolume(double length, double width, double height) {
return (length * width * height);
}
// gets input and clculates volume
float PrintVolume() {
float lenght, width, height, volume;
printf("enter length, width, height values");
scanf("%f", &lenght);
scanf("%f", &width);
scanf("%f", &height);
volume = calculateVolume(lenght, width, height);
return volume;
}
int main(int argc, char** argv) {
printf("%f", PrintVolume());
return 0;
}
2. Prints on separate lines 10 9 8
3. A
#include<stdio.h>
int main(int argc, char** argv) {
int i = 0, sum = 0, n;
float avg;
printf("Enter 10 integers to find sum and average");
for (i = 0; i < 10; i++) {
scanf("%d", &n);
sum = sum + n;
}
avg = sum / 10.0;
printf("%f", avg);
return 0;
}
3. B
#include<stdio.h>
int main(int argc, char** argv) {
int i = 0, sum = 0, n;
float avg;
printf("Enter 10 integers to find sum and average");
do{
scanf("%d", &n);
sum = sum + n;
i++;
}while(i<10);
avg = sum / 10.0;
printf("%f", avg);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.