1. Write an interactive program that asks the user to input the length and width of a rectangular lawn. The dimensions should be in yards. Your program should compute the area of the lawn in square yards, square feet, and square inches. Print all the information neatly on the screen. Use the following declaration:
int cv_factor = 36 * 36; /*conversion: sq in per yrd */
An equivalent declaration is:
int cv_factor = 1296; /* conversion: sq in per yrd */
but because most people know that there are 36 inches in a yard, the first declaration is preferable. One can tell at a glance that the right conversion factor is being used. Caution: If your lawn is large and you are working on a small machine, you might not get the right number of square inches, even though you wrote a correct program. There are limitations on the size of an integer that can be stored in an int.
Answer:
#include <stdio.h>
int main(){
int cv_factor=36*36, cv_factor2=3*3, length, width, areain, areaft, areayd;
printf("Please enter the length of the lawn in yards followed by the width.");
scanf(“%d%d”, length, width);
areayd=(length*width);
areaft=(areayd*cv_factor2);
areain=(areayd*cv_factor);
printf ("Area in yards: %d",areayd);
printf ("Area in feet: %d",areaft);
printf (“Area in inches: %d",areain);
}
2. Consider the following code:
int a = 1, b = 2, c = 3;
a += b += c += 7;
Write an equivalent statement that is fully parenthesized. What are the values of the variables a, b, and c? First write down your answer; then write a test program that will check it.
Answer:
#include <stdio.h>
Int main (){
int a=1, b=2, c=3;
c_prime=(c+7);
b_prime=(c_prime + b);
a_prime=(a + b_prime);
printf (“c: %d”,c_prime);
printf (“b: %d”,b_prime);
printf (“a: %d”,d_prime);
}
4. Write a program that prints out the first n primes, where n is the input by the user. When you execute your program, here is an example of what you should see printed on the screen:
PRIMES WILL BE PRINTED.
How many do you want to see? 3000
1: 2
2: 3
3: 5
4: 7
5: 11
…..
25: 97
26: 101
…..
2998: 27431
2999: 27437
3000: 27449
Write your program in its own directory in three files: primes.h, main.c, and is_prime.c. Here is some of the program:
In file primes.h:
#include <stdio.h>
#include <stdlib.h>
int is_prime(int n);
In file is_prime.c:
#include “primes.h”
int is_prime(int n)
{
int k, limit;
if (n = = 2)
return 1;
if (n % 2 = = 0)
return 0;
limit = n / 2;
for (k = 3; k <= limit; k + = 2)
if (n % k = = 0)
return 0;
return 1;
}
Explain how is_prime( ) works. Hint: Do some examples by hand. Complete the program by writing main( ) in main.c. There are 168 primes that are less than 1000. Does your program confirm this? How many primes are there that are less than 10,000?
Answer:
#include <stdio.h>
#include <stdlib.h>
#include "is_prime.c"
int main(){
int n=1, count2=0, final;
printf("PRIMES WILL BE PRINTED.\n")
printf("How many do you want to see? \n")
scanf(“%d”,final);
printf(“%d”,n)
while(count2!=final){
n++;
if(is_prime(n)!=0){
count2++;
printf(“%d”,count2)
printf(“%d”,n)
}
}}
Yes my program confirms, There are 1,229 primes less than 10,000.
5. The ancient Egyptians wrote in hieroglyphics, in which vowel sounds are not represented, only consonants. Is written English generally understandable without vowels? To experiment, write a function isvowel ( ) that tests whether or not a character is a vowel. Use your function in a program that reads the standard input file and writes to the standard output file, deleting all vowels. Use redirection on a file containing some English text to test your program.
Answer:
#include <stdio.h>
#include <fstream>
#include <cstdlib>
using namespace std;
int isVowel(char);
int main(){
char a[1]=1000;
ifstream in_statement;
in_statement.open("statement.txt");
for(i=0; i<1000; i++){
in_statement>>a[i];}
in_statment.close();
isVowel(a);
}
int isVowel(int a[i]){
for(i=0; i<1000; i++){
if(a[i]!='a'&&a[i]!='e'&&a[i]!='i'&&a[i]!='o'&&a[i]!='u'){
ofstream out_statement;
out_statement.open("outstatement.txt");
out_statement<<a[i];
out_statemtn.close();
}
}
}
6. In the game of paper, rock, scissors, an outcome that is not a tie is conveyed to the player by printing:
You win. Or You lose.
Rewrite the program so that messages like the following are printed:
You chose paper and I chose rock. You win.
Answer:
p_r_s.h
/* The game of paper, rock, scissors. */
#include <ctype.h> /*for isspace() */
#include <stdio.h> /* for printf(), etc */
#include <stdlib.h> /* for rand() and srand() */
#include <time.h> /* for time() */
Enum p_r_s {paper, rock, scissors,
game, help, instructions, quit};
enum outcome {win, lose, tie, error};
typedef enum p_r_s p_r_s;
typedef enum outcome outcome;
outcome compare(p_r_s player_choice, p_r_s machine_choice);
void prn_final_status(int win_cnt, int lose_cnt);
void prn_game_status(int win_cnt,
int lose_cnt, int tie_ctn);
void prn_help(void);
void prn_instructions(void);
void report(outcome result, int *win_cnt_ptr,
int *lose_cnt_ptr, int *tie_cnt_ptr);
p_r_s selection_by_machine(void);
p_r_s selection_by_player(void);
main.c
#include “p_r_s.h”
int main(void)
{
int win_cnt = 0, lose_cnt = 0, tie_cnt = 0;
outcome result;
p_r_s player_choice, machine_choice;
srand(time(NULL)); /* seed the random number generator */
prn_instructions();
while ((player_choice = selection_by_player()) != quit)
switch (player_choice) {
case paper:
case rock:
case scissors:
machine_choice = selections_by_machine();
result = compare(player_choice, machine_choice)
report(result, &wincnt, &lose_cnt, &tie_cnt);
break;
case game:
prn_game_status(win_cnt, lose_cnt, tie_cnt);
break;
case instructions:
prn_instructions();
break;
case help:
prn_help();
break;
default:
printf(“{RPGRAMMER ERROR: Cannot get to here!\n\n”);
exit(1);
}
prn_game_status(win_cnt, lose_cnt, tie_cnt);
prn_final_status(win_cnt, lose_cnt);
return );
}
select.c:
#include “p_r_s.h”
p_r_s selection_by_machine(void)
{
return ((p_r_s) (rand() % 3));
}
p_r_s selection_by_player(void)
{
char c’
p_r_s player_choice;
printf(“Input p, r, or s: “);
scanf(“%c”, &c);
switch (c) {
case ‘p’:
player_choice = paper;
break;
case ‘r’:
player_choice = rock;
break;
case ‘s’:
player_choice = scissors;
break;
case ‘g’:
player_choice = game;
break;
case ‘i’:
player_choice = instructions;
break;
case ‘q’:
player_choice = quit;
break;
default:
player_choice = help;
break;
}
return player_choice;
}
compare.c:
#include “p_r_s.h”
Outcome compare(p_r_s player_choice, p_r_s machine_choice)
{
outcome result;
if (player_choice == machine_choice)
return tie;
switch (player_choice) {
case paper:
result = (machince_choice == rock) ? win : lose;
break;
case rock:
result = (machine_choice == scissors) ? win : lose;
break;
case scissors:
result = (machine_choice == paper) ? win : lose;
break;
default:
printf(“PROGRAMMER ERROR: Unexpected choice!\n\n”);
exit(1):
}
return result;
}
report.c:
#include “p_r_s.h”
void report(outcome result, int *win_cnt_ptr,
Int *lose_cnt_ptr, int *tie_cnt_ptr)
{
switch(result) {
case win:
++*win_cnt_ptr;
printf(“%27s i chose”);
printf(“%c”, player_choice);
printf(“%27s you chose”);
printf(“%c”, machine_choice);
printf(%27sYou win.\n”, ””);
break;
case lose:
++*lose_cnt_ptr;
printf(“%27s i chose”);
printf(“%c”, player_choice);
printf(“%27s you chose”);
printf(“%c”, machine_choice);
printf(%27sYou lose.\n”, ””);
break;
case tie:
++*tie_cnt_ptr;
printf(“%27sA tie.\n”, “”);
break;
default:
print(“PROGRAMMER ERROR: Unexpected result!\n\n”);
exit(1);
}
}
7. Write a function that shifts the stored value of five character variables in a circular fashion. Your function should work in the following way. Suppose that c1,c2,…, c5 are variables of type char, and suppose that the values of these variables are ‘A’, ‘B’, …, ‘E’, respectively. The function call shift(&c1, &c2, &c3, &c4, &c5) should cause the variables c1, c2, …, c5 to have the values ‘B’, ‘C’, ‘D’, ‘E’, ‘A’, respectively. Your function definition should start as follows:
void shift(char *p1, char *p2, char *p3, char *p4, char *p5)
{ …..
Test your function by calling it five times and printing it out, in turn, BCDEA, CDEAB, DEABC, EABCD, and ABCDE.
Answer:
#include<stdio.h>
Int main(){
Int n=0, cycles;
printf(“How many times to shift?”);
scanf(“%d”,cycles);
while(n!=cycles){
void shift(char *p1, char *p2, char *p3, char *p4, char *p5){
char a, b, c, d, e;
a=*p1; b=*p2; c=*p3; d=*p4; e=*p5;
printf(“%c%c%c%c%c”,a,b,c,d,e)
n++;
}
}