EXERCISE


Chapter 1

1. Outline a program that calculates regular and over-time
wages based on weekly hours.

INPUT
Input the number of regular hours worked
Input the number of overtime hours worked
Input the hourly rate of pay

PROCESS
Multiply the regular hours times the rate of pay
Multiple the overtime hours times the rate of pay times 1.5
Add the two results

OUTPUT
Display the result.

2. Outline a program that determines if a person is eligible to
retire. (The retirement age is 65.)

INPUT
Input the person's age

PROCESS
Compare the age with 65

OUTPUT
If the age is 65 or more, display a message that the person
is eligible to retire.
If the age is less than 65, display a message that the
person is not eligible to retire.


Chapter 2

1. Write a program that displays the following on the screen:

Welcome to my world.
Please don't rain on my parade.



puts("Welcome to my world.\n");
puts("Please don't rain on my parade.\n");


2. Write a program that displays your name, address, and
telephone number, centered on the screen.


puts(" A. Aardvark\n");
puts(" 111 Albany Avenue\n");
puts(" Altoona, Alaska 09987\n");
puts(" 123-555-1234\n");



Chapter 3

1. Decide what types of data you need, and write their
declarations, for a program that computes the total weekly
salary of an individual who gets double-time for every hour
over 40 per week.


float payrate, reg_hours, o_hours, total;

Use floats for reg_hours and o_hours if you want to pay for
partial hours, otherwise use int types.

2. Decide what types of data you need, and write their
declarations, for a program that computes the sum and
average of four numbers.


If decimal numbers:

float number_1, number_2, number_3, number_4, sum, average;

If integer numbers:

int number_1, number_2, number_3, number_4, sum;
float average;



3. Explain what is wrong in the following program segment:

char client[3]="Ajax";
main()
float tax_due;
char name(10);
int count(5);

Four characters are assigned to the string client, even
thought it is declared so it can only store two characters.
The declaration for the string name uses parentheses rather
than brackets. The declaration for count uses parentheses
rather than brackets, and is declared as an int type rather
than char.



Chapter 4

1. Write the puts() instructions for printing your name and
address.

puts("A. Aardvark");
puts("111 Albany Avenue");
puts("Altoona, Alaska 09987");


2. Write the printf() instructions for printing your name and
address.

printf("A. Aardvark\n");
printf("111 Albany Avenue\n");
printf("Altoona, Alaska 09987\n");

3. Write the puts() instruction that centers the word Title on
the screen. The screen is 80 characters wide.

puts(" Title\n")


4. Write the printf() instruction that displays the word Page
on the far right side of the display.

printf("%80s","Page");



5. Write one printf() instruction that outputs the values of
the following variables:

float length, width, height, volume;

printf("%f %f %f %f", length, width, height, volume);

6. A program will display a person's name and age. Write a
printf() instruction that displays the values of the
variables:
char name[12];
int age

printf("%s is %d years old", name, age);

7. A program has these variables:
char item[] = "Floppy disk";
float cost = 3.55;
float markup = 0.75;

Write the printf() instructions that displays:

Item Name: Floppy disk
Item Cost: 3.55
Markup : 0.75

Note the alignment of the values.


printf("Item Name:%17s\n",item);
printf("Item Cost:%17.2f\n",cost);
printf("Markup :%17.2f\n",markup);

8. A program has the following variable:

int count = 30;

Write the output instructions that sound a bell, then
display the following, using the value of the variable count
to display the number in the last line:

Warning! Warning! Warning! Warning!;
An intruder has been detected.
You have 30 seconds to leave the premises.

putchar('\007');
printf("Warning! Warning! Warning! Warning!\n");
printf("An intruder has been detected.\n");
printf("You have %d seconds to leave the premises.", count);



Chapter 5

1. Write a program that inputs your name and telephone number,
then displays them on one line on the screen.

main()
{
char name[25];
char telephone[12];
printf("Please enter your name: ");
gets(name);
printf("Please enter your telephone number: ");
gets(telephone);
printf("Name: %s Phone number: %s", name, telephone);
}

2. Write a program that inputs a number, then displays the
address where the number is stored in memory.

main()
{
int number;
printf("Please enter an integer number: ");
scanf("%d", &number);
putchar('\n');
printf("The number %d is stored at %d", number, &number);
}


3. Write a program that inputs three numbers, then displays the
numbers in reverse order- -the last number input is
displayed first, etc.

main()
{
int num_1, num_2, num_3;
printf("Please enter the first integer number: ");
scanf("%d", &num_1);
printf("Please enter the second integer number: ");
scanf("%d", &num_2);
printf("Please enter the third integer number: ");
scanf("%d", &num_3);
printf("3: %d 2: %d 1: %d", num_3, num_2, num_1);
}

4. Write a program that uses getchar(), gets(), and scanf().

main()
{
char first[10], last[15];
char initial;
int age;
printf("Enter your first name: ");
gets(first);
printf("Enter your middle initial: ");
initial = getchar();
putchar('\n');
printf("Enter your last name: ");
gets(last);
printf("Enter your age: ");
scanf("%d", &age);
printf("Name: %s %c %s\n", first, initial, last);
printf("Age: %d", age);
}



5. Explain what is wrong with the following program.

main()
{
char initial;
initial = gets();
puts(initial);
}

The variable initial is declared as a single character. A
character must be input using the getchar() function and
displayed with the putchar() or the printf() function.

Chapter 6

1. Write a program that tells a person how old they will be in
the year 2000.

main()
{
int year, age, togo;
printf("Enter the year, as in 1993: ");
scanf("%d", &year);
printf("Enter your age as a whole number: ");
scanf("%d", &age);
togo = 2000 - year + age;
printf("In the year 2000 you will be %d years old", togo);
}


2. Write a program that calculates the square and cube of a
number input on the keyboard.

main()
{
int number, square, cube;
printf("Enter an integer number: ");
scanf("%d", &number);
square = number * number;
cube = number * number * number;
printf("The number is %d\n", number);
printf("The number squared is %d\n", square);
printf("The number cubed is %d\n", cube);
}


3. Write a program that converts a fahrenheit temperature to
celsius. The formula is celsius = (5.0/9.0)*(fahrenheit-32).


main()
{
int temp;
float celsius;
printf("Enter a temperature, an integer please: ");
scanf("%d", &temp);
celsius = (5.0/9.0)*(temp-32);
printf("Fahrenheit: %d Celsius: %f", temp, celsius);
}



4. Modify the program in Exercise 3 to report how many degrees
the input temperature is from the freezing point in both
fahrenheit and celsius.

main()
{
int temp, ffreeze;
float celsius;
printf("Enter a temperature, an integer please: ");
scanf("%d", &temp);
celsius = (5.0/9.0)*(temp-32);
ffreeze = temp - 32;
printf("Fahrenheit: %d To freezing: %d\n", temp, ffreeze);
printf("Celsius %f To freezing: %f", celsius, celsius);
}

5. Explain what is wrong in the following program.

#define tax_rate 0.06
main()
{
float cost, total;
printf("Enter the cost of the item: ");
scanf("%f", &cost);
printf("Enter the shipping charge: ");
scanf("%f", &shipping)'
total = cost + cost * tax_rate + shipping;
printf("The total is %f", total);
}

The variable shipping is used in the program but it is not
declared. The second scanf() instruction ends with an
apostrophe instead of a semicolon.


Chapter 7


1. Write a quiz program that asks four questions, each question
and answer in a different function.

char pause;
main()
{
quest1();
quest2();
quest3();
quest4();
}

quest1()
{
puts("What is in the center of a cell?\n");
puts("Press Enter for the answer\n");
pause=getchar();
puts("The nucleus\n");
return;
}

quest2()
{
puts("How can you get a tapeworm?\n");
puts("Press Enter for the answer\n");
pause=getchar();
puts("From eating undercooked meat\n");
return;
}

quest3()
{
puts("What are the major blood types?\n");
puts("Press Enter for the answer\n");
pause=getchar();
puts("The major types are A, B, AB, and O\n");
return;
}

quest4()
{
puts("What is the French word for sea?\n");
puts("Press Enter for the answer\n");
pause=getchar();
puts("In France, the sea is called la mer\n");
return;
}

2. Write a program that inputs a numbers, then uses a function
to calculate and display the number to the fourth power.

main()
{
int number;
printf("Enter an integer number: ");
scanf("%d", &number);
tothefourth(number);
}

tothefourth(value)
int value;
{
int power;
power=value*value*value*value;
printf("%d to the fourth power is %d", value, power);
return;
}


3. Convert the program in Exercise 2, so the calculation to the
fourth power is calculated in a function, but the results
are passed back to main() to be displayed.

main()
{
int number, fourth;
printf("Enter an integer number: ");
scanf("%d", &number);
fourth = tothefourth(number);
printf("%d to the fourth power is %d", number, fourth);
}

int tothefourth(value)
int value;
{
return(value*value*value*value);
}

4. Explain what is wrong with the following program.

dothis()
{
puts("This is first");
main();
return(0);
}
main()
{
puts("This is second");
return();
}

Custom functions must appear following main(), not before
it. Custom functions can not call main(). Edit the program
as follows:

main()
{
puts("This is first");
dothis();
return(0);
}
dothis()
{
puts("This is second");
return(0);
}



Chapter 8

1. Write a program that inputs a number, then reports whether
the number if odd or even.

main()
{
int number, remain;
printf("Enter a number: ");
scanf("%d", &number);
remain=number % 2;
if(remain==0)
puts("The number is even");
else
puts("The number is odd");
}


2. Write a program that inputs a number, then reports whether
the number is in the range from 1 to 100.

main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number > 0 && number <= 100)
puts("The number is between 1 and 100");
}


3. Write a program that inputs a integer number, then reports
which range the number is in: below 0, from 0 to 50, 51 to
100,101 to 150, or above 150.

main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if(number < 0)
puts("The number is negative");
else
if(number > 0 && number < 51)
puts("The number is between 0 and 50");
else
if(number > 50 && number <= 101)
puts("The number is between 51 and 100");
else
if(number > 101 && number < 151)
puts("The number is between 101 and 150");
else
puts("The number is above 150");
}



4. Write a program that asks the user to enter numbers into the
variables lownum and highnum. The value of lownum should be
less than the value of highnum. If that is not the case, the
program should exchange the numbers, placing the low value on
lownum and the high value in highnum. Display the values to
confirm the operation.


main()
{
int lownum, highnum, temp;
puts("Please enter two numbers. The first number should \n");
puts("be lower than the second number\n");
printf("Enter the lower number: ");
scanf("%d", &lownum);
printf("Enter the higher number: ");
scanf("%d", &highnum);
putchar('\n');
if(lownum < highnum)
puts("You entered the numbers in correct order\n");
else
{
temp = highnum;
highnum = lownum;
lownum = temp;
puts("You entered the numbers incorrectly\n");
puts("I have corrected the problem myself.\n");
}
printf("The value of lownum is %d\n", lownum);
printf("The value of highnum is %d\n", highnum);
}

5. Explain what is wrong with the following program:

main()
{
int age;
printf(Enter your age);
scanf("%f", &age);
if age < 18 then
puts("You cannot vote");
else
if age > 18 then
puts("You can vote");
}

There are no quotation marks around the literal in the first
printf() statement. The variable age is declared as an int but
input through scanf() as a float. There are no parentheses around
the conditions in both if statements, and the word then must be
deleted. The program will not display a message when the user is
18 years old, only when either younger or older than 18.



Chapter 9

1. Edit the program shown in Listing 8.10 (Chapter 8) so it
repeats until the user has no additional input.

main()
{
float rate, hours, total, regular, extra, d_time, overtime;
int moredata;
do
{
printf("Enter your hourly rate of pay: ");
scanf("%f", &rate);
printf("Enter the number of hours you worked: ");
scanf("%f", &hours);
d_time=rate * 2;
if (hours <= 40)
{
regular = hours * rate;
extra = 0.0;
overtime = 0.0;
total = regular;
}
else
{
regular = 40 * rate;
extra = hours - 40;
overtime = extra * d_time;
total = regular + overtime;
}
putchar('\n');
printf("Your regular weekly salary is %.2f\n", regular);
printf("You worked %.2f overtime hours\n", extra);
printf("Your overtime rate is $%.2f\n", d_time);
printf("Your overtime pay is %.2f\n", overtime);
printf("Your total pay is %.2f\n", total);
printf("Do you want to compute another salary? Y or N: ");
moredata = getchar();
putchar('\n');
}
while(moredata=='y' || moredata=='Y');
}


2. Write a program that displays a 6 percent sales tax table
for sales from $1 to $50, in this format:

Cost Shipping Total
1 $.06 $1.06
2 $.12 $1.12

main()
{
int cost;
float shipping, total;
puts("Cost\tShipping\tTotal\n");
for(cost=1;cost<51;cost++)
{
shipping = cost * 0.06;
total = cost + shipping;
printf("$%d\t$%.2f\t\t$%6.2f\n",cost, shipping, total);
}
}


3. Write a program that inputs 10 numbers between zero and 25.

main()
{
int count, number;
for(count=1;count<11;count++)
{
printf("Entering number %d", count);
putchar('\n');
do
{
printf("Enter a number from 0 to 25: ");
scanf("%d",&number);
}
while(number<0 || number > 25);
putchar('\n');
}
}


4. Write a program that displays this graphic:
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *

main()
{
int outer,inner;
for(outer=5;outer>0;outer--)
{
for(inner=1;inner<=outer;inner++)
printf("*");
putchar('\n');
}
for(outer=2;outer<6;outer++)
{
for(inner=1;inner<=outer;inner++)
printf("*");
putchar('\n');
}
}


5. Explain what is wrong with the following program.

main()
{
float row, column;
puts("\t\tMy Handy Multiplication Table\n\n");
for(row=1; row <=10;row++)
{
for(column=1; column<=10;column+)
printf("%d", row*column);
}
putchar('\n');
}

The variables row and column are declared as floats but they
must be int types to be used in the for loop. The putchar('\n')
instruction is in the incorrect location. In this position, it
will only execute after the entire table is printed on one line.
Move the instruction up before the closing brace in the line
above.


Chapter 10

1. Write a program that uses arrays to store the names,
addresses, and telephone numbers of 20 persons. Store the
names in first name, last name order.

main()
{
char names[20][20], street[20][20], city[20][20];
char state[20][3], zip[20][6], phone[20][13], lookfor[20];
int count;
for(count=0;count<20;count++)
{
puts("Enter a name, first name first");
gets(names[count]);
puts("Enter the street address");
gets(street[count]);
puts("Enter the city");
gets(city[count]);
puts("Enter the state");
gets(state[count]);
puts("Enter the zipcode");
gets(zip[count]);
puts("Enter the phone number");
gets(phone[count]);
}
}

2. Modify the program in Exercise 1 to input a name, and then
search the arrays for the person's telephone number.

main()
{
char names[20][20], street[20][20], city[20][20];
char state[20][3], zip[20][6], phone[20][13], lookfor[20];
int count;
for(count=0;count<20;count++)
{
puts("Enter a name, first name first: ");
gets(names[count]);
puts("Enter the street address: ");
gets(street[count]);
puts("Enter the city: ");
gets(city[count]);
puts("Enter the state: ");
gets(state[count]);
puts("Enter the zipcode: ");
gets(zip[count]);
puts("Enter the phone number: ");
gets(phone[count]);
}
puts("Enter name to look for: ");
gets(lookfor);
for(count=0; count<20;count++)
{
if(strcmp(names[count],lookfor)==0)
printf("%s %s\n", names[count], phone[count]);
}
}


3. Explain what is wrong with the following program.
main()
{
int temps(31);
int index, total;
for(index=0; index < 31; index++)
{
printf("Enter temperature #%d:", index);
scanf("%d",&temps(index));
}
high=temps(0);
low=temps(0;
index=1;
while(index< 31)
{
if(temps(index)>high)
high=temps(index);
else
low=temps(index);
index++;
}
printf("low is %d\n",low);
printf("high is %d\n",high);
}

All of the temps array subscripts use parentheses rather
than brackets. The variables high and low are not declared, and
the variable total is declared but not used in the program. There
should be an if condition following the else statement. Just
because the value is not higher than the previous high value does

not mean that it is necessarily lower than the previous low.


Chapter 11

1. Write a program that inputs product inventory information
into a structure. The information includes the product name,
cost, quantity, and the supplier name.

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item;
main()
{
puts("Enter product information\n\n");
printf("Enter the name:");
gets(item.name);
printf("Enter the cost:");
scanf("%f", &item.cost);
printf("Enter the quantity");
scanf("%d", &item.quantity);
printf("Enter the vendor:");
gets(item.vendor);
}


2. Modify the program in Exercise 1 to input information into a
20 element array of the structure.

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item[20];
main()
{
int count;
for(count=0;count<20;count++)
{
puts("\nEnter product information\n\n");
printf("Enter the name:");
gets(item[count].name);
printf("Enter the cost:");
scanf("%f", &item[count].cost);
printf("Enter the quantity");
scanf("%d", &item[count].quantity);
printf("Enter the vendor:");
gets(item[count].vendor);
}
}

3. Modify the program in Exercise 2 to display the total value
of the inventory.

struct product
{
char name[20];
float cost;
int quantity;
char vendor[20];
} item[20];
main()
{
float total;
int count;
total=0;
for(count=0;count<20;count++)
{
puts("\nEnter product information\n\n");
printf("Enter the name: ");
gets(item[count].name);
printf("Enter the cost: ");
scanf("%f", &item[count].cost);
printf("Enter the quantity: ");
scanf("%d", &item[count].quantity);
printf("Enter the vendor: ");
gets(item[count].vendor);
total = total + (item[count].cost * item[count].quantity);
}
printf("The total value of inventory is %8.2f", total);
}


4. Write a program that inputs two float variables local to
main(), then uses a function to square both numbers.

main()
{
float num1, num2;
puts("Enter the first number");
scanf("%f",&num1);
puts("Enter the second number");
scanf("%f",&num2);
doubleit(&num1, &num2);
}

doubleit(dcount1, dcount2)
float *dcount1, *dcount2;
{
float sq1, sq2;
sq1 = *dcount1 * *dcount1;
sq2 = *dcount2 * *dcount2;
printf("the square of %f is %f\n",*dcount1, sq1);
printf("the square of %f is %f\n",*dcount2, sq2);
}


To pass the values back to the calling routine, use this program:

main()
{
float num1, num2;
puts("Enter the first number");
scanf("%f",&num1);
puts("Enter the second number");
scanf("%f",&num2);
doubleit(&num1, &num2);
printf("The square of the first number is %f\n", num1);
printf("The square of the second number is %f", num2);
}

doubleit(dcount1, dcount2)
float *dcount1, *dcount2;
{
float sq1, sq2;
*dcount1 = *dcount1 * *dcount1;
*dcount2 = *dcount2 * *dcount2;
}



5. Explain what is wrong with the following program.

main()
{
struct CD
{
char description[20];
char category[40];
char name[20];
float cost;
int number;
} disc;
puts("Enter disk information");
printf("Enter the name:");
gets(name);
printf("Enter the description:");
gets(description);
printf("Enter the category:");
gets(category);
printf("Enter the cost:");
scanf("%f", &cost);
printf("Enter the slot number:");
scanf("%d", &number);
puts("The information on the CD is:");
printf("Name: %s\n",name);
printf("Description: %s\n",description);
printf("Category: %s\n",category);
printf("Cost: %6.2f\n",cost);
printf("Location: %d\n",number);
}

None of the variables used in input and output commands are
referenced with the structure variable. They should be referred
to as disc.name, disc.description, and so on.

Chapter 12

1. Write a program that uses fputs() to create a file of movie
titles.

#include "stdio.h"
main()
{
FILE *fp;
char flag;
char title[20];
if((fp = fopen("MOVIES","w"))==NULL)
{
puts("Cannot open the file");
exit();
}
flag = 'y';
while(flag!='n')
{
puts("Enter a movie title: ");
gets(title);
fputs(title, fp);
fputs("\n",fp);
printf("Do you have another title to enter?");
flag=getchar();
putchar('\n');
}
fclose(fp);
}

2. Write a program that reads the movie titles (Exercise 1)
into a string array.

#include "stdio.h"
main()
{
FILE *fp;
int index;
char titles[80][12];
index = 0;
if((fp = fopen("MOVIES","r"))==NULL)
{
puts("Cannot open the file");
exit();
}
while(fgets(titles[index],12,fp)!= NULL)
{
puts(titles[index]);
index++;
if(index>80)
{
puts("Sorry, this is set to read up to 80 titles");
break;
}
}
fclose(fp);
}

3. Write a program that uses fprintf() to create a product
inventory file containing the product name, cost, and
quantity.


#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","w"))==NULL)
{
puts("Cannot open the file");
exit();
}
puts("\nEnter product information\n\n");
printf("Enter the name: ");
gets(item.name);
while(strlen(item.name)>0)
{
printf("Enter the cost: ");
scanf("%f", &item.cost);
printf("Enter the quantity: ");
scanf("%d", &item.quant);
fprintf(fp, "%s %f %d\n", item.name, item.cost, item.quant);
printf("Enter the name: ");
gets(item.name);
}
fclose(fp);
}



4. Write a program that reads the file created in Exercise 3.



#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","r"))==NULL)
{
puts("Cannot open the file");
exit();
}
while(fscanf(fp,"%s %f %d", item.name, &item.cost, &item.quant) != EOF)
{
printf("Item: %s\n", item.name);
printf("Cost %f\n", item.cost);
printf("Quantity: %d\n", item.quant);
}
fclose(fp);
}


5. Edit the programs created for Exercises 3 and 4 to read and
write the data as a structure.


#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","w"))==NULL)
{
puts("Cannot open the file");
exit();
}
puts("\nEnter product information\n\n");
printf("Enter the name: ");
gets(item.name);
while(strlen(item.name)>0)
{
printf("Enter the cost: ");
scanf("%f", &item.cost);
printf("Enter the quantity: ");
scanf("%d", &item.quant);
fwrite(&item, sizeof(item), 1, fp);
printf("Enter the name: ");
gets(item.name);
}
fclose(fp);
}





#include "stdio.h"
main()
{
FILE *fp;
struct product
{
char name[20];
float cost;
int quant;
} item;
if((fp = fopen("MYFILE","r"))==NULL)
{
puts("Cannot open the file");
exit();
}
while(fread(&item, sizeof(item), 1, fp)==1)
{
printf("Item: %s\n", item.name);
printf("Cost %f\n", item.cost);
printf("Quantity: %d\n", item.quant);

}
fclose(fp);
}





6. Explain what is wrong in the following program.

#include "stdio.c"
main()
{
FILE fp;
char letter;
if((fp = fopen("MYFILE","w"))==NULL)
{
puts("Cannot open the file");
exit();
}
do
{
letter=getchar();
fputc(letter, cfp);
}
while(letter!='\n');
fclose(fp);
}

The name of the header file is "stdio.h", not "stdio.c". The
file pointer must have an asterisks in the declaration, as in
*fp. The while condition must use the \r code, not the \n code.



Wyszukiwarka

Podobne podstrony:
PASSIVE VOICE revision exercises
exercisespastsimplepastcontinuous
Exercise Programs for Children with Cerebral Palsy
basic combat training military vocabulary exercise
LabA Exercise2?d1997
Middle Pillar Exercise
countable uncountable exercises
LabA Exercise1 CreateCube
exercice questceque vous faites le samedi francuski24 com
ExercisSeqStratXSection
B06 exercise04 2 DJ
Volleyball Functional Exercises
exercice jamais
Consecutive Exercises
Facial Exercise for Look Younger
wish grammar exercise

więcej podobnych podstron