PROGRAMMING EXERCISES 1. Write a program that shows how an integer has a \"boole
ID: 3576655 • Letter: P
Question
PROGRAMMING EXERCISES
1. Write a program that shows how an integer has a "boolean" value. Ask the user to enter an integer and use if ( number ) ... to print TRUE when the value of the integer is "true", otherwise print FALSE. Explain the rule that associates any integer with true/false. [In your program only use : if ( number ) ... Do not use variations such as if ( number == 1 ) ...
2. Write a program to experiment with const. (a) Declare a variable with the const qualifier and initialize it. Print it, then increase it, and print it again. What happens when you compile your program? (b) Do the same with a string, and try to change one of its characters. Make sure that your programs work correctly by also removing const.
3. (a) Write the following function with a single exit (ie one return statement). DO NOT USE BREAK! Read about it in the Textbook: Part D - Functions - Design Principles (and also on the Web). (b) Can you use const with the parameters? Explain why.
It is important to note that the function does not check all the numbers in the array - it stops when it finds the first odd number; your single exit version must do the same.
bool allEven(int data[], int n)
{
int i ;
for ( i=0 ; i<n ; i++ )
if ( data[i] % 2 != 0 )
return false;
return true;
}
4. (a) Compile this program with 3 functions. Do you get any errors? Explain. Read about "Lifetime of a Variable". (b) What happens if you declare x in g - using: int x; ?
int main(void)
{
int x = 7;
f();
return 0;
}
void f(void)
{
int x = 5;
g(x);
}
void g(int n)
{
printf( "%d ", x+n );
}
5. FOR FUN: Write a short C program that appears to do nothing - there is no input or output - but finally stops after about 1 minute. Do not use: sleep(60).
Explanation / Answer
1)
#include "stdio.h"
int main(void) {
int i;
scanf("%d",&i);
if(i)
printf("TRUE ");
else
printf("FALSE ");
return 0;
}
/*
Any non-zero number equals TRUE
Zero equals FALSE
*/
2)
#include "stdio.h"
#include "string.h"
int main(void) {
// Disable stdout buffering
setvbuf(stdout, NULL, _IONBF, 0);
int i=5;
printf("i -> %d ",i);
i = i+1;
printf("i -> %d ",i);
char str[] = "Hello";
printf("%s ",str);
str[1] = 'c';
printf("%s ",str);
return 0;
}
/*
const int incrementaion has compilation error i.e. assignment of read-only variable
const string char replace operation has compilation error. i.e assignment of read-only location.
*/
3)
bool allEven(int data[], int n)
{
int i ;
for ( i=0 ; i<n ; i++ )
if ( data[i] % 2 != 0 )
exit(0);
return true;
}
/*
const can be used with te parameters, only thing about const is it's value cannot be altered.
*/
4)
#include<stdio.h>
void g(int n)
{
int x;
printf( "%d ", x+n );
}
void f(void)
{
int x = 5;
g(x);
}
int main(void)
{
int x = 7;
f();
return 0;
}
/*
The g function has an error, x is not declared,
the function g cannot access x which is in function f or function main.
The scope of a varaiable inside a function is limited to itself, it's not visibile outside.
Declaring x in the function g would produce the output 5.
because in the main f is called, which in turn passes 5 to function g, in g x is declared,which makes x=0. and n will have 5, so the output is 5.
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.