Ali Jivraj CPS Lab 02
Problem 1:
Program Error Message Cause of Error Correction Applied
program01.c main.[Link] error: “Double” in all caps Re-wrote double in
unknown type name lowercase
‘DOUBLE’
program02.c main.[Link] warning: No “&” in front of Added “&” in front of
format ‘%lf’ expects variable y to indicate it variable y
argument of type is of type double
‘double *’, but
argument 2 has type
‘double’ [-Wformat=]
program03.c main.[Link] error: User defined function Changed “aver” to “ave”
incompatible types was not returning the
when returning type local variable “ave”
‘double (*)(double, and was returning
double)’ but ‘double’ “aver” instead.
was expected
program04.c main.[Link] warning: Printf statement was Changed “aver” to
format ‘%lf’ expects using variable “aver” “result”
argument of type instead of “result”
‘double’, but argument
2 has type ‘double
(*)(double, double)’
program05.c main.[Link] error: “&” symbol in front of x Removed “&” symbol in
incompatible type for when being put into front of x on line 32
argument 1 of ‘aver’ the “aver” function
when x is already
defined as a double.
program06.c No error message The math in the Added brackets around
average function is n1 and n2
Testing revealed that wrong as it calculates
the average of 2 and 4 n1 + n2/2 instead of
is 4 which is false (n1+n2)/2
program07.c main.[Link] error: Missing “{“ at Added “{“ at start of
expected declaration beginning of user aver function (line 12)
specifiers before ‘ave’ defined function for
average Changed “int” to
main.[Link] error: “double” at line 10
expected declaration User defined function
specifiers before uses integer instead of
‘return’ double
main.[Link] error:
expected declaration
specifiers before ‘}’
token
main.[Link] error:
expected ‘=’, ‘,’, ‘;’,
‘asm’ or ‘__attribute__’
before ‘{’ token
main.[Link] error:
old-style parameter
declarations in
prototyped function
definition
main.[Link] error:
expected ‘{’ at end of
input
program08.c main.[Link] error: Average function is Changed return(result)
‘result’ undeclared (first returning a variable to return(ave)
use in this function that does not exist
locally in the function.
program09.c main.[Link] error: Missing “;” at end of Added “;” at end of line
expected ‘;’ before line 32 32
‘printf’
program10.c main.[Link] error: Missing opening Added “(“ after printf
expected ‘;’ before bracket after printf command
string constant statement
main.[Link] error:
expected statement
before ‘)’ token
program11.c main.[Link] error: N1 and N2 are not Swap x and y with n1
‘n1’ undeclared (first defined by the and n2
use in this function) function.
main.[Link] error:
‘n2’ undeclared (first
use in this function)
program12.c main.[Link] warning: 2.21 next to %lf should Removed 2.21 from
unknown conversion not be included in the scanf command.
type character ‘.’ in scanf function only the
format printf function
main.[Link] warning:
too many arguments
for format
main.[Link] warning:
unknown conversion
type character ‘.’ in
format
main.[Link] warning:
too many arguments
for format
program13.c main.[Link] error: too The program only calls Changed “result = aver
few arguments to the function “aver” with (x); “ to “result = aver
function ‘aver’ one input when the (x,y);”
function needs two
inputs to work.
Problem 2:
Calculating the speed of sound in air at a given temperature.
Algorithm:
1. Get user input for degrees in Celsius
2. Convert degrees Celsius to degrees fahrenheit.
3. Input the degrees fahrenheit into the formula for speed of sound.
4. Use a conversion factor to change the units of speed of sound from ft/s to km/h.
5. Display the final result
Code:
#include <stdio.h>
#include <math.h>
// Function to calculate speed of sound in km/h
double
spsound(double temp)
{
double degF, speedFTS, speedKMH;
// Convert Celsius to Fahrenheit
degF= (temp*1.8)+32;
// Calculate speed of sound in feet per second
speedFTS= 1086 * sqrt((5 * degF + 297) / 247);
// Convert speed from feet per second to km/h
speedKMH= 1.09728*speedFTS;
return(speedKMH);
}
int
main()
{
double x;
printf("Enter a temperature in degrees Celsius to find the speed of sound in air: ");
// Get temperature in degrees Celsius from user
scanf("%lf", &x);
// Calculate speed of sound and print answer
printf("The speed of sound at %.2lf degrees Celsius is %.2lf Km/h", x, spsound(x));
return(0);
}
Output:
Enter a temperature in degrees Celsius to find the speed of sound: 12
The speed of sound at 12.00 degrees Celsius is 1802.28 Km/h
Screenshot: