Assignment 1
1) Create 3 files:
main.cpp, mymath.h mymath.cpp
mymath.cpp should contain a single function whose
declaration looks like:
int sum( int x1, int x2 )
It takes 2 integer variables and returns their sum.
mymath.cpp will contain the definition for the function.
"mymath.h" will contain only the declaration for the function
"main.cpp" will include the file "mymath.h" . It will contain the
main function and inside the main function it will contain a line:
printf( "The sum of %d and %d is %d\n" , b1 , b2, sum(b1 +b2 ) ) ;
Compile the programs using the command:
gcc main.cpp mymath.cpp
and run the executable. Take a snapshot of the screen showing the output.
Attach the snapshot and the 3 files and upload them .
Assignment 2
1 acres equals 43560 sq ft. Write a single file "C" program that asks the user to enter a number in acres and converts the number to square ft . A user may enter a fraction such as ".17" . The sq ft should only print at most 2 decimal fraction digit. So it should print "3.14" and not "3.1425678" as an example.
A sample run might be:
Enter the acres to convert: . 17
0.17 acres are 7405.20 sq ft
Assignment 3
Number Guessing Game
This program plays a game where you guess a number
between 1 and 100 and the computer will try to guess the number.
The strategy that the computer uses is to keep two variables ; call them
low and high that start out with low=1 and high=100 . It then computes
the mid point ( in this case ((1+100) /2) = 50 ) . It then prompts the
user with it's guess and the user will state if it is correct , higher or
lower than the number the user has guesses. Now the user will state the number
is lower. Now the computer changes the high to be 49 an the new mid point is
25. The computer will keep adjusting the low and the high. If the number is lower
then the high is midpoint - 1 and if the number is higher then the low is adjusted
to be midpoint +1 .
The first thing the program should do is prompt the user:
Ready to play (y/n)?
Then if the user selects "y" a game is started.
The computer then prompts the user:
"Think of a number between 1 and 100."
Then the computer will state:
"My guess is " currentGuess ". Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:"
The user will select the letters 'l' , 'h' or 'c' . You can use the "getchar" function to get the character.
Once the computer guesses the number then the computer will
prompt the user if the user wants to play again.
Great! Do you want to play again (y/n)?
Assume the user guesses the number 30 then a sample run might be .
Ready to play (y/n)? y
Think of a number between 1 and 100.
My guess is 50. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:l
My guess is 25. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:h
My guess is 37. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:l
My guess is 31. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:l
My guess is 28. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:h
My guess is 29. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:h
My guess is 30. Enter 'l' if your number is lower, 'h' if it is higher, 'c' if it is correct:c
Great! Do you want to play again (y/n)? n
Assignment 4
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x1 ;
int* ptr1 ;
int* ptr2 ;
int arr1[] = { 1, 2, 3 } ;
int* arr2 ;
int* arr3 ;
x1 = 100 ;
ptr1 = &x1 ;
//TO DO
//Using only ptr1 variable increment the value of x1 by 1
printf("x1 value is %d\n" , x1 );
//TO DO
//Using only the pointer notation and not the [] index notation
//print out the elements of arr1 .
printf( "\n" ) ;
//TO DO
//Define a single integer dynamically and assign the address to
// ptr2 . Assign a value of 100 to this allocated memory and print it out
//along with the address .
return ( 0 ) ;
}