Question

1- Correct the syntax errors in the following program, and rewrite the program so that it...

1- Correct the syntax errors in the following program, and rewrite the program so that it follows the proper style conventions. What does the program do? (Write it out as a comment). (10 pts)

#include <stdio.h>

int

main() {/*

* Calculate and display the difference of two values

*)

int X, /* first input value */ x, /* second

input value */

sum; /* sum of inputs */

X=9,

x=7.5

X + x = sum;

printf("%d + %d = %d\n"; X; x; sum); return (0);}

2- In the same main function as above, and right below the first question, before the return (0); statement, write another program that calculates the average of 3 numbers. (10 pts )

a) This program takes 3 numbers of type int as input. Declare and initialize the integer variables with your choice of names and values.

b) Declare another variable of type double to store the average of three numbers into.

c) Calculate the average of the 3 input variables and store them in the variable that you declared as type double.

d) Make sure that your average value calculations are accurate! (Hint: Use type casting)

e) Use printf to display all 3 values and their average. Your output should look similar to this:

The average of three values 2, 2 and 3 is 2.333333.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1- Corrected code

#include <stdio.h>

int main() {/*

* Calculate and display the difference of two values

*/

int X,x; /* first input value */ /* second

input value */

int sum; /* sum of inputs */

X=9;

x=7.5;

sum = X + x ;

printf("%d + %d = %d\n", X, x,sum); return (0);}

Output:

9 + 7 = 16

2-

#include <stdio.h>

int main() {/*

* Calculate and display the difference of two values

*/

int X,x; /* first input value */ /* second

input value */

int sum; /* sum of inputs */

X=9;

x=7.5;

sum = X + x ;

printf("%d + %d = %d\n", X, x,sum);
  
  
   int a,b,c;
   a = 12;
   b = 15;
   c = 20;
  

double avg;

avg = (double)(a+b+c)/3;

printf("The average of three values %d, %d and %d is %lf ",a,b,c,avg);


return (0);
}

Output:

9 + 7 = 16
The average of three values 12, 15 and 20 is 15.666667 

Do ask if any doubt. Please up-vote.

Add a comment
Know the answer?
Add Answer to:
1- Correct the syntax errors in the following program, and rewrite the program so that it...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /*...

    /*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /*    */ /* */ /* This program computes average power, average magnitude */ /* and zero crossings from a speech signal. */ #include <stdio.h> #include <math.h> #define MAXIMUM 50 int main(void) { /* Declare variables */ int k=0, npts=30; double speech[MAXIMUM]={0.000000,-0.023438,-0.031250,-0.031250,-0.039063,-0.039063,-0.023438,0.000000,0.023438,0.070313,-0.039063,-0.039063,0.046875,0.101563,0.117188,0.101563,0.070313,0.054688,0.023438,0.000000,-0.031250,-0.039063,-0.070313,-0.070313,-0.070313,-0.070313,-0.062500,-0.046875,-0.039063,-0.031250}; /* Declare the function prototypes */    /* Compute and print statistics. */ printf("\n SPEECH DATA "); print(,,);//complete the statement printf("\n"); printf(" SPEECH STATISTICS : \n"); printf(" average power: %f \n",);//complete the statement printf(" average magnitude: %f...

  • The following program has some syntax errors. Enumerate the errors then rewrite, compile and run the...

    The following program has some syntax errors. Enumerate the errors then rewrite, compile and run the program after correction. #include <stdio.h> int Main( ); { float w ; float wk ; printf ("Enter your weight in pounds "); scanf["%d" weight];    wk=w x 0.45359237 ; printf('Your weight in kilograms is %d',wk); }

  • 1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf...

    1 Rewrite the following program so that it will use function. include <stdio.h> Int main) printf ("Hello!") return 0 2 Assume a program has the following declarations: short s- 4 int i--5; long m-3 float f-19.3f; double d-3.6; What are the following values? (a) s+m (b) (float) d (c) d s (e) (int) f 3 What will be the output of the following program: int x = 3 float a 2.76; y-2* x; print f("X-2d, y*ta, z»%d", x, y, z);...

  • C program help: Write a C program that uses three functions to perform the following tasks:...

    C program help: Write a C program that uses three functions to perform the following tasks: – The first function should read the price of an item sold at a grocery store together with the quantity from the user and return all the values to main(). example: #include void getInput(int *pNum1, int *pNum2); // note: *pNum1 & *pNum2 are pointers to ints int main () {    int numDucks,numCats;    getInput(&numDucks, &numCats); // passing addresses of num1 & num2 to...

  • C program. Using do-while loop for this question. Question: write a program to calculate the average...

    C program. Using do-while loop for this question. Question: write a program to calculate the average of first n numbers. output: Enter the value of n : 18 The sum of first 18 numbers =171 The average of first 18 numbers = 9.00 my code couldn't give me the right avg and sum. Please help. #include<stdio.h> int main() {     int num;     int count =0;     int sum=0;     float avg;      printf("Enter the value of n: ");    ...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays...

    Create a program using Visual Studio 2017 called dataExercise.c that declares the following variables and displays their values: 1. Declare a character variable with value 'a', and display the character using printf with a %c format. Then add a second printf that displays the character with a %d format. 2. Declare a short int variable with a value set to the maximum value of a short int (the maximum value for whatever machine I happen to use to grade your...

  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • Fix all syntax and logical errors for the following program. Please generate the correct output. //...

    Fix all syntax and logical errors for the following program. Please generate the correct output. // Program Typos prints three integer numbers, sums the numbers, calculates // the average, and prints the sum and the average of the three numbers. #Include #Include using namespace std; const ONE = 5; const TWO = 6; const THREE = 7; int Main () int sum; float average; cout << fixed << showpoint; c0ut << SetW(5) << ONE << TWO << THREE << end1;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT