C programming 1a If int n1 5 and int d1 2 what are the res
C programming
1.a If int n1 = 5, and int d1 = 2, what are the results of the following
operations? (assume double frac)
a. frac = (double)n1/(double)d1;
b. frac = (double)n1/d1 + 3.5;
c. frac = (double)(n1/d1) + 2.5;
b. What\'s wrong, if anything, with the following function call:
scanf(%d,first);
2. a Show which of the following are valid and which are invalid identifiers: Why?
run_1
2run
double
Char
double-trouble
two*four
b. What\'s wrong with the following function call:
(3 dots means I’m not showing all of the program steps)
#include
double find_scale(double x, int n);
.
.
.
int main(void)
{
double num_1, num_3;
int num_2;
.
.
.
num_3 = Find_scale(num_1, num_2);
.
.
.
return(0);
}
double find_scale(double x, int n)
{
.
.
.
return(0);
}
3. a Suppose you enter the following condition with x equal to 5, does the conditions below evaluate to true or false? Why?
if(x <= 0 || x = 1)
if(x%2 != 2)
b. Give an example of a preprocessor directive.
c.Explain what the program counter (PC) is used for in the CPU of a computer.
4. a What is the value of b following execution of the following expression in c? Assume a = 5, b = 7, c = 3.2,
d = 5.0.
int a,b:
double c,d;
b = (b/a)*c + c*d;
b. Is the following case structure correct? If incorrect - what\'s wrong?
int name;
.
.
.
switch(name){
case 1;
.
.
.
break;
case 2;
.
.
.
break;
case 3;
.
.
.
break;
default;
.
.
.
}
5. Convert the decimal number 75 to binary.
6. Write a recursive function that calculates the value of a number (“base”) raised to a power (“power”). Assume that power is a nonnegative integer. Document your program
******** Please show the steps and work**********
Solution
1.a If int n1 = 5, and int d1 = 2, what are the results of the following
operations? (assume double frac)
a. frac = (double)n1/(double)d1;
Result=2.500000
b. frac = (double)n1/d1 + 3.5;
Result=6.000000
c. frac = (double)(n1/d1) + 2.5;
Result=4.500000
b. What\'s wrong, if anything, with the following function call:
scanf(%d,first);
Answer:The syntax provided is wrong. Correct syntax is scanf(\"%followed by equivalent rep of variable type\",&followed by variable name)
in you case, it should be scanf(\"%d\",&first);
2. a Show which of the following are valid and which are invalid identifiers: Why?
run_1----valid
2run-----invalid as identifier can\'t be followed by integer
double---invalid as double is keyword nad it throws delcarion specifiers error
Char----valid as char is a keywork and C being cas-sensitive,considers char and Char as different
double-trouble---invalid as it throws declaration specifiers err
two*four--invalid as it consider two and fours as two different indetifers and considers to apply * on them.
b. What\'s wrong with the following function call:
(3 dots means I’m not showing all of the program steps)
#include
double find_scale(double x, int n);
.
.
.
int main(void)
{
double num_1, num_3;
int num_2;
.
.
.
num_3 = Find_scale(num_1, num_2);
.
.
.
return(0);
}
double find_scale(double x, int n)
{
.
.
.
return(0);
}
Answer: find_scale() is the function name whereas the function call has Find_scale. As c is case-sensitive,it considers it as different and trys to find the fucntion with name \"Find_scale\".
3. a Suppose you enter the following condition with x equal to 5, does the conditions below evaluate to true or false? Why?
if(x <= 0 || x = 1)
Answer: This won\'t compile as the second condition is specified wrongly.It should be corrected as if(x <= 0 || x == 1).
if(x%2 != 2)
Asnwer: This returns true. Because 5%2=>1 which isn\'t equal to 2
b. Give an example of a preprocessor directive.
preprocessor directive are lines included in a program preceeded by a #. Example of such is header files like #include <stdio.h>
c.Explain what the program counter (PC) is used for in the CPU of a computer.
Answer: PC contains the address of the instruction that is currently being executed. It increases the stored value by 1 as and when each instruction gets fetched.
4. a What is the value of b following execution of the following expression in c? Assume a = 5, b = 7, c = 3.2,
d = 5.0.
int a,b:
double c,d;
b = (b/a)*c + c*d;
Answer :19
b. Is the following case structure correct? If incorrect - what\'s wrong?
int name;
.
.
.
switch(name){
case 1;
.
.
.
break;
case 2;
.
.
.
break;
case 3;
.
.
.
break;
default;
.
.
.
}
Answer: The structure specified is wrong because each case should be ended with a \":\" but not \";\" Default switch structure is
switch(variable)
{
case variable_value: ......
break;
case variable_value:...break;
default:
}
And for the othe questions, please post separate questions





