Uploaded by elishalm

Ryerson CPS393 Exam Practice: Programming Questions

advertisement
Programming Practice Questions
1. [6] Complete the following C program which reads from a data file the length
and width of a rectangle, and the computes its area. See sample data file below.
Program requirements:
 have your program expect the data file rectangle.txt
 assume input data file is error-free (file exists, no invalid characters)
 Print the dimensions read in and area to two decimal places.
 Match your program’s output to the example below.
Example:
$ cat rectangle.txt
3.48 16.2
$ rectarea
length = 3.48
width = 16.20
Area = 56.38
$
#include <stdio.h>
int main (void) {
float length, width, area;
FILE *infile;
infile = fopen ("rectangle.txt", "r");
fscanf(infile, "%f", &length);
fscanf(infile, "%f", &width);
area = length * width;
printf("length = %.2f\n", length);
printf("width = %.2f\n", width);
printf("Area = %.2f\n", area);
fclose(infile);
Page 1 of 9
} /* end main
*/
2. [8] Complete the following C program which computes the average of each row
of a 2-dimensional array and prints out all values.
$ arr2d
Table values: average values
-5
6
3:
1.33
-3
-4
9:
0.67
#include <stdio.h>
#define NUMROWS 2
#define NUMCOLS 3
int main (void) {
int table[NUMROWS][NUMCOLS]={{-5,6,3},{-3,-4,9}};
float average[NUMROWS];
int i, j, sum;
for (i=0; i< NUMROWS; i++) {
sum=0;
for (j=0; j<NUMCOLS; j++) {
sum += table[i][j];
} /* end for j */
average[i]=(float)sum/NUMCOLS; /* need to cast one operand
float */
/* see ihypress example 2-2 (integer division)*/
} /* end for i */
printf("Table values: average values\n");
for (i=0; i< NUMROWS; i++) {
for (j=0; j<NUMCOLS; j++)
printf("%5d", table[i][j]);
printf(":%9.2f\n", average[i]);
PR1
Page 2 of 9
} /* end for i */
return (0);
} /* end main
PR1
*/
Page 3 of 9
6. [8] Complete the following C program which adds two measurements in pounds
and ounces. Prompt the user to enter the measurement values. Assume whole
numbers only. Store each measurement and total in the given C structure
containing a separate member (field) for pounds and ounces values. Adjust the
ounces so that it is always less than one pound. (There are 16 ounces in 1
pound.) Match your output to the sample dialog below.
$ ./poundsounces
This program will add two measurements in pounds and
ounces.
Enter number of pounds for first measurement: 2
Enter number of ounces for first measurement: 9
Enter number of pounds for second measurement: 3
Enter number of ounces for second measurement: 12
Total is: 6 pounds 5 ounces.
#include <stdio.h>
#define OZINLBS 16
struct measurement {
int pounds;
int ounces;
};
int
main (void)
{
struct measurement meas1, meas2, meas_total;
printf("This program will add two measurements in
pounds and ounces. \n");
printf("Enter number of pounds for first
measurement: ");
scanf("%d", &meas1.pounds);
printf("Enter number of ounces for first
measurement: ");
scanf("%d", &meas1.ounces);
printf("Enter number of pounds for second
PR1
Page 4 of 9
measurement: ");
scanf("%d", &meas2.pounds);
printf("Enter number of ounces for second
measurement: ");
scanf("%d", &meas2.ounces);
meas_total.pounds = meas1.pounds + meas2.pounds;
meas_total.ounces = meas1.ounces + meas2.ounces;
if (meas_total.ounces >= OZINLBS)
{
meas_total.pounds = meas_total.pounds +
(meas_total.ounces /
OZINLBS);
meas_total.ounces = meas_total.ounces % OZINLBS;
}
printf("Total is: %d pounds %d ounces.\n",
meas_total.pounds,
meas_total.ounces);
PR1
Page 5 of 9
return (0);
}
PR1
Page 6 of 9
7. [8] Write a C program which will look for the occurrence of a character within
a word and identify its position. Your program should accept two command
line arguments, the first being the character to look for, and the second, the
word in which to look. (If the character occurs more than once, just find the
first time it occurs). Match your code the sample dialog below:
$ findchar n misunderstanding
The n character is in position 5.
$ findchar 1 stacey123
The 1 character is in position 7.
$ findchar b easy
The b character is not in the given word.
#include <stdio.h>
#include <string.h>
main (int argc, char *argv[]) {
char chartolookfor;
int position, foundposition=0;
chartolookfor=argv[1][0];
/* take first char of 1st
argv */
for (position=0; position < strlen(argv[2]);
position++) {
if (chartolookfor == argv[2][position]) {
foundposition=position+1; /* need +1 to correct
for zero-based */
break;
} /* end if */
} /* end for */
printf("There %c character is ", chartolookfor);
if (foundposition != 0)
printf("in position %d.\n", foundposition);
else
printf("not in the given word.\n");
PR1
Page 7 of 9
} /* end main */
8. [8] Referring to the diagram and code below, write a set of C language
statements which will
a) declare variables to hold data from the diagram below
b) populate the data structures with the values shown in the diagram
c) link the structures together to form a linked list.
Add lines of code below the structure type definition. You do not have to write
a complete program.
carmake1
[Toyota| - ]--->
^
|
|
[-]
start
PR1
carmake2
[Ford| - ]--->
Page 8 of 9
carmake3
[Audi| - ]-ground (null)
#define MAXWORDLEN 15
/* any reasonable number here is fine */
struct node_make {
char name[MAXWORDLEN]; /* don't hard code size */
struct node_make *next;
};
struct node_ make carmake1, carmake2, carmake3;
struct node_ make *start;
strcpy(carmake1.name, "Toyota");
strcpy(carmake2.name, "Ford");
strcpy(carmake3.name, "Audi");
carmake1.next = &carmake2;
carmake2.next = &carmake3;
carmake3.next = NULL;
start = &carmake1;
PR1
Page 9 of 9
Download