Вы находитесь на странице: 1из 15

CE 102

Computer Programming II
SPRING 2015

REVIEW

What is the output of the following


program, assuming that the address
of x is 1000, the address of a is 2000,
address of p is 3000?

#include <stdio.h>
int *p, x,y;
int a[5]={ 100,200,300,400,500};
int *p2;
int main()
{
p=NULL;
x=10;
p=&x;
printf("1) %d %d %p %p %p \n",x,*p,p,&x,&p);
p2=&x;
printf("2) %d %d %p %p \n",x,*p2,p2,&x);
p2=a;
printf("3) %d %d %p %p \n",a[0],*p2,p2,a);
p2=&a[2];
printf("4) %d %p %p \n",*p2,p2,a);
p2++;
printf("5) %d %p \n",*p2,p2);
p=a;

Write a C function that searches for


value key in a a 2D array of size m
by 5. The function should return the
row number and col number of
location the value was found at if
found and return -1 if not found.

Write a C function that searches for


value key in a specific row R in a 2D
array of size m by n. The function
should return true if found false
otherwise.

Write a function that takes inputs of yards


and feet (whole numbers) and calculates
and returns an output of the total number
of miles (a floating-point value). There are
5280 feet per mile. There are 3 feet in a
yard. Use appropriate parameter passing
and return mechanisms. Use appropriate
data types. For example, with inputs of
1760 yards and 1320 ft, the result would
be 1.25 miles. Call this function from the
main function with different sets of values
and print the output.

Write down a complete C program that performs


the following
Define a struct type called student with members
grade (of type int), name (of type char[20])
Define an array called ce102students of size 20
with type struct student.
Read grades and names for 20 students using
scanf. The reading process should be done using
loop. The values of grade should be in the range
of 0 to 100 inclusive.
Calculate the average of the grades.
Calculate the highest grade and display the
name of the person who has the highest grade.

Given the following series: 1 2 5 26


677 ..... such that the nth value
equals to (n-1 th)2 +1 and the first
value is 1. Write a recursion function
named f to compute the nth value.

The results of a survey of the households in


your township have been made available.
Each record contains data for a one
household, including an integer
identification number, the annual income
for the household, and the number of
members of the household. You may
assume that no more than 25 households
were surveyed. Write a program to store
the survey results into an array of userdefined structures of type household_t.
Then, perform the following analyses using
functions:

a) Print a three column table displaying the data.


b) Calculate the average household income, and
list the identification number and income of
each household whose income exceeds the
average.
c) Determine the percentage households having
incomes below the poverty level. The poverty
level income maybe calculated using the
formula:
P = $7500 + $950 * ( m 2)
Where m is the number of members of each
household.

d) Write the survey results into a text file.

Write a function that will merge the


contents of the two text files
containing chemical elements sorted
by atomic number and will produce
a sorted file of text records. The
function parameters will be three file
pointers. Each text file line will
contain an integer atomic number
followed by the element name,
chemical symbol and atomic weight.
Here are two sample lines:
11 Sodium NA 22.99

Given the following structure and variable definitions:


struct customer {
char lastName[ 15 ];
char firstName[ 15 ];
int customerNumber;
struct {
char phoneNumber[ 11 ];
char address[ 50 ];
char city[ 15 ];
char state[ 3 ];
char zipCode[ 6 ];
} personal;
} customerRecord, *customerPtr;
customerPtr = &customerRecord;

write an expression that can be used to access the structure members in


each of the following parts:
a) Member lastName of structure customerRecord.
b) Member customerNumber of the structure pointed to by customerPtr.
c) Member address of member personal of structure customerRecord.

(Packing Characters into an Integer) The leftshift operator can be used to pack two character
values into an unsigned integer variable. Write a
program that inputs two characters from the
keyboard and passes them to function
packCharacters. To pack two characters into an
unsigned integer variable, assign the first character
to the unsigned variable, shift the unsigned
variable left by 8 bit positions and combine the
unsigned variable with the second character using
the bitwise inclusive OR operator. The program
should output the characters in their bit format
before and after they are packed into the unsigned
integer to prove that the characters are in fact
packed correctly in the unsigned variable.

(Unpacking Characters from an Integer) Using the


right-shift operator, the bitwise AND operator and a
mask, write function unpackCharacters that takes the
unsigned integer from Exercise 10.13 and unpacks it
into two characters. To unpack two characters from an
unsigned in- teger, combine the unsigned integer with
the mask 65280 (00000000 00000000 11111111
00000000) and right shift the result 8 bits. Assign the
resulting value to a char variable. Then combine the
unsigned integer with the mask 255 (00000000
00000000 00000000 11111111). Assign the result to
another char variable. The program should print the
unsigned integer in bits before it is unpacked, then
print the characters in bits to confirm that they were
unpacked correctly.

Вам также может понравиться