(1)Write a C program that does the following:
The session should look something like:
Enter an integer:
You entered the integer 34
(2)Write a C program that asks the user for an integer input, and then decides if the input is positive, negative or zero.
Here's a sample session:
UNIX prompt >> a.out
(3)Write a C program that goes through a FOR loop 3 times, asking the user for an integer input each time through the loop and deciding whether the input was positive, negative, or zero.
Here's what a sample session would look like:
UNIX prompt >> a.out
(4)Repeat (3) but this time use a WHILE loop that executes until the input is zero.
Sample Output:
UNIX prompt >>a.out
Enter an integer: 1
#include
void main( ) { int a = 2; printf("Value of 'a' in main( ) before calling MyFunction( ) is %d\n", a); MyFunction(a); printf("Value of 'a' in main( ) after calling MyFunction( ) is %d\n", a); } void MyFunction(int a) { a = 9; printf("Value of 'a' inside MyFunction( ) is %d\n", a); }
The program has the following output:
UNIX prompt >> a.out
Value of 'a' in main( ) before calling MyFunction( ) is 2Why is the value of 'a' different in MyFunction( ), and unchanged in main( )?
(6)Consider the following variable declarations:
int *a;
int **b;
int ***c;
Fill in the following table with the appropriate data types:
(7)Convert the code in (5) to pass by reference. Verify that the value of "a" permanently changes after the function timestwo().
(8)Write a program in C to implement a simple calculator, which does the following operations:
Add, Subtract, Multiply, Divide, Sin, Cos, Tan.
Note:
Sin, Cos, Tan are defined in the header file math.h
Therefore, in the beginning of the program, just below
you'd also need to include#include
#include
Compile the program as follows:
gcc –lm calculator.c
Sample Outputs:
UNIX prompt >> a.out
VALID OPERATIONS:Select the operation number you wish to perform (1 - 7): 5
Enter the operand: 0.7854Run it once more
UNIX prompt >> a.out
VALID OPERATIONS:Select the operation number you wish to perform (1 - 7): 5
Enter the two operands: 2 4
You entered 2.000000 and 4.000000
Answer = 6.000000
1 0 0 1 0 1 0 1
1 0 1 1 1 0 1 1
0 1 0 0 1 0 0 0
1 1 1 0 0 1 1 1
…
The first seven digits (bits) of each byte contain the information that is to be transmitted (called the message part of the byte). The last bit of each byte is called the check bit; it's used as a crude check to determine if the data was transmitted correctly, or there was an error during transmission.
error --> a zero transmitted as one, or a one transmitted as a zero.
Here's how the check bit is used to detect transmission errors:
If there is an even number of one's in the message part of the byte, the check_bit should be 0
If there is an odd number of one's in the message part of the byte, the check_bit should be 1
Your job:
Sample session
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 0 0 1 0 1 0 1
Transmission successful!
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 0 1 1 1 0 1 1
Transmission successful!
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 0 1 0 0 1 0 0 1
Transmission Error!
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 1 1 0 0 1 1 0
Transmission Error!
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 1 1 0 0 1 1 1
Transmission successful!
(10)Repeat (9); this time the program should keep asking the user for input (Hint: use a loop) until the user enters all zeros, that is, the input is: 0 0 0 0 0 0 0 0
Would you use a FOR loop or a WHILE loop? Why?
Sample output:
UNIX prompt >> a.out
Enter a sequence (length 8) of zero's and one's: 1 1 1 1 1 1 1 1
Transmission successful!
Enter a sequence (length 8) of zero's and one's: 1 1 0 0 0 0 0 1
Transmission Error!
Enter a sequence (length 8) of zero's and one's: 1 0 1 0 1 0 1 0
Transmission successful!
Enter a sequence (length 8) of zero's and one's: 1 0 0 1 0 0 0 1
Transmission Error!
Enter a sequence (length 8) of zero's and one's: 0 0 0 0 0 0 0 0
Transmission successful!
EXITING...