Description
Part 2 – Using Variables and Arithmetic
For the second part of the program we will be using variables and math. Helpful link: printf
● Part A
- Uncomment the remainder of the code, and add the following expressions shown here in the main:
- 3x3 – 5x2 + 6 for x = 2.5.
- The result of (4 × 108 + 2 × 10-7) / (7 × 10-6 + 2 × 108)
- read the comments in the code to know where to write your code
- To round off an integer i to the next largest even multiple of another integer j, the following formula can be used:
- int next_multiple = i + j – i % j
- For example, to round off 256 days to the next largest number of days evenly divisible by a week, values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
int next_multiple = 256 + 7 – 256 % 7
- For example, to round off 256 days to the next largest number of days evenly divisible by a week, values of i = 256 and j = 7 can be substituted into the preceding formula as follows:
- int next_multiple = i + j – i % j
- Write a function called findNextMultiple(int number1, int number2) to find the next largest even multiple for the following values of i and j:
- i j
365 7
12258 28
996 4
- i j
- Write a function, float convertFtoC(float fahrenheit), that converts 40° from degrees Fahrenheit (F) to degrees Celsius (C) using the following formula and returns the result:
- C = (F – 32) / 1.8
● Part B
- In the next part of the program we are going to see how choosing the wrong data types and careless casting can result in data loss. You should see inaccurate results.
- Write functions to typecast a long integer to the following datatypes
- int
- double
- char
- Write functions to typecast a long integer to the following datatypes
● Part C
- In the Fibonacci sequence, the first two Fibonacci numbers, called f0 and f1, are defined to be 0 and 1, respectively. Thereafter, each successive Fibonacci number fi is defined to be the sum of the two preceding Fibonacci numbers fi2 and fi So fi2 is calculated by adding together the values of fi0 and fi1.
- Write a function that generates the first 20 Fibonacci numbers (including 0 and 1) using a loop.
- You should return the final resulting value
Part 3 – Submission
- Required code organization:
- <user_id>_program1.c
- While inside your program1 folder, create a zip archive with the following command
- zip -r <user_id>_program1 <user_id>_program1.c
- This creates an archive of all file and folders in the current directory called <user_id>_program1.zip
- Do not zip the folder itself, only the files required for the program
- Upload the archive as specified for the course
- zip -r <user_id>_program1 <user_id>_program1.c
#include<stdio.h>
#include <math.h>
#include <assert.h>
//Function to calcualte next multiple of two given values
int findNextMultiple(int number1, int number2){
//TODO
int next_multiple;
next_multiple = number1 + number2 – number1%number2;
printf(“Next Multiple of the number is: %d\n”, next_multiple);
}
//Function to convert Fahrenheit to Celsuius
float convertFtoC(float fahrenheit){
//TODO
float Celsius;
Celsius = (fahrenheit – 32) / 1.8;
printf(“Temperature in Fahrenheit is: %f\n”, Celsius);
}
//Function to calculate fibonacci series for first 20 numbers
int fibonacci(int n){
//TODO
int fib1=0, fib2=1;
int next_number;
if (n<=1)
return n;
for (int i=1; i<=n; i++)
{
printf(“%d “, fib2);
next_number = fib1 + fib2;
fib1 = fib2;
fib2 = next_number;
}
}
//Function to cast a long to an int
int castToInt(long num){
//TODO
}
//Function to cast a long to an double
double castToDouble(long num){
//TODO
}
//Function to cast a long to an char
char castToChar(long num){
//TODO
}
//The main driver
int main(){
//TODO Write code to print “Hello World”
printf(“\tHello World\n”);
//TODO For given value of x calculate value of expression
printf(“\n\t=============================\n”);
printf(“\t= PART A =\n”);
printf(“\t=============================\n\n”);
printf(“\n\t=========Starting Expression Conversion Tests===========\n”);
//Write code to evaluate the following expression: 3x^3 – 5x^2 + 6 for x = 2.5.
//float result = 0; //TODO write your expression here
double l,x,y;
x = 2.55;
float result = 3*pow(x,3) – 5*pow(x,2) + 6;
//assert(result == 21.625);
printf(“Value of the expression 3x^3-5x^2+6 for x(2.5) = %f\n”, result);
//Write code to evaluate the following expression: (4 * 10^8 + 2 * 10^-7) / (7 * 10^-6 + 2 * 10^8)
//int result1 = 0;//TODO write your expression here
int result1 = (4*pow(10,8) + 2*pow(10,-7))/(7*pow(10,-6) + 2*pow(10,8));
//assert(result == 2.0);
printf(“Value of the expression (4 × 10^8 + 2 × 10^-7) / (7 × 10^-6 + 2 × 10^8) = %e”, result1);
printf(“\n\t\t….Converting Expressions Tests Passed\n”);
printf(“\n\t=========Starting Next Multiple Tests===========\n”);
//For given numbers find next multiple
int number1 = 365, number2 = 7;
//assert(371 == findNextMultiple(number1,number2));
findNextMultiple(number1,number2);
number1 = 12258; number2 = 28;
//assert(12264 == findNextMultiple(number1,number2));
findNextMultiple(number1,number2);
number1 = 996; number2 = 4;
//assert(1000 == findNextMultiple(number1,number2));
findNextMultiple(number1,number2);
printf(“\n\t\t….Next Multiple Tests Passed\n”);
printf(“\n\t=========Starting Fahrenheit to Celsius Tests===========\n”);
//Convert Fahrenheit value to Celsius value
float fahrenheit = 95;
float celsius = convertFtoC(fahrenheit);
//assert(35.0 == celsius);
fahrenheit = 32;
celsius = convertFtoC(fahrenheit);
//assert(0.0 == celsius);
fahrenheit = -40;
celsius = convertFtoC(fahrenheit);
//assert(-40.0 == celsius);
printf(“\n\t\t….Fahrenheit to Celsius Tests Passed\n”);
printf(“\n\t=============================\n”);
printf(“\t= PART B =\n”);
printf(“\t=============================\n\n”);
printf(“\n\t=============================\n”);
printf(“\t= PART C =\n”);
printf(“\t=============================\n\n”);
printf(“\n\t=========Starting Fibonacci Tests===========\n”);
//finding fibonacci series for first 20 numbers
printf(“%d”, fibonacci(20));
//assert(4181 == fibonacci());
printf(“\n\t\t….Fibonacci Tests Passed\n”);
printf(“\n\t=========ALL TESTS PASSED===========\n”);
return 0;
}