473,327 Members | 1,979 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Are These Correct? Any adjustments? C Programming

10
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++;
}
}
Dec 6 '06 #1
4 2961
willakawill
1,646 1GB
Hi. Do they work?
Dec 6 '06 #2
alisbub
10
Hi. Do they work?
for some reason I cant get my C programming program to work on my computer. Plus I am on vacation so I dont have my own computer. Any recommendations.
Dec 6 '06 #3
willakawill
1,646 1GB
for some reason I cant get my C programming program to work on my computer. Plus I am on vacation so I dont have my own computer. Any recommendations.
It might be that somebody on this site with a lot of time on their hands is willing to do your assignments for you. Other than that you are stuck until you return.
Dec 6 '06 #4
alisbub
10
It might be that somebody on this site with a lot of time on their hands is willing to do your assignments for you. Other than that you are stuck until you return.
ok Thanks for trying though
Dec 6 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
10
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include...
10
by: Alf P. Steinbach | last post by:
The fifth part of my attempted Correct C++ tutorial is now available, although for now only in Word format (use free Open Office if no Word), and also, it's not yet been reviewed at all -- ...
24
by: Alf P. Steinbach | last post by:
The eighth chapter (chapter 2.1) of my attempted Correct C++ tutorial is now available, although for now only in Word format -- comments welcome! Use the free & system-independent Open Office...
53
by: Alf P. Steinbach | last post by:
So, I got the itch to write something more... I apologize for not doing more on the attempted "Correct C++ Tutorial" earlier, but there were reasons. This is an UNFINISHED and RAW document,...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
4
by: Mary | last post by:
Please direct me to the correct ng for ASP newbies. Thank you.
3
by: martinharvey | last post by:
This is probably a very simple question but i would appreciate some help with the correct syntax for and update stored procedure I have created a user form that allows the user to update the...
2
Paul NIcolai Sunga
by: Paul NIcolai Sunga | last post by:
i have a project in my java programming and database class we are bound to connect the database and the java netbeans programming language its just that i am so confused about the statements cause...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.