Docsity

CGS 3460 Assignment 5: Programming Using C, Assignments of Computer Science

Toggle sidebar
bg1
CGS 3460 - Programming Using C
Summer 2009
Assignment #5
20 Points
Assigned: 06/12/2009, Friday
Due: 06/19/2009, Thu, 11:59 PM
1. (5 Points) Write a program that reads in 11 integers from the user and puts them
into an array in the order entered by the user. The program will then send this
array to a FUNCTION which calculates and prints the mean and median. The mean
is the sum of the numbers divided by 11. The median is the midpoint value (half of
the numbers are above it, and half are below it). To calculate the median, the
array must be sorted. You may want to consider the quicksort function defined in
chapter 9 of the course textbook.
Save this as p1.c
2. (5 Points) Write a program that reads an integer, N, from the user. The program
then calls a function with the following function declaration N times.:
void functionForProb2( );
This function will print the message “I've been called %d times\n”, where %d is the
actual number of times the function has been called. The function can only use
variables declared within the function. i.e. no global (external) variables and no
parameters are sent to the function.
Save this as p2.c
3. (5 Points) Write a program which defines the following function.
void get_rational(int *num, int *den)
This function should read a rational number from the user. The rational number
should be entered in the form “i/j” where i and j are integers. It should return the
numerator(i) in num and the denominator in den. The main function will then print
out both the numerator and the denominator.
Save this as p3.c
4. (5 Points) This part is not a programming assignment that will be compiled. I am
asking you to trace the code manually. You could code, compile and run these
programs to get the answer, but as you will not have a computer available to you
on the exam, I would not suggest this. Put the answers to these in a text file called
p4.txt.
a) What will be the output to the following program.
void swap(int a, int b);
pf3

Partial preview of the text

Download CGS 3460 Assignment 5: Programming Using C and more Assignments Computer Science in PDF only on Docsity!

CGS 3460 - Programming Using C

Summer 2009

Assignment # 20 Points Assigned: 06/12/2009, Friday Due: 06/19/2009, Thu, 11:59 PM

  1. (5 Points) Write a program that reads in 11 integers from the user and puts them into an array in the order entered by the user. The program will then send this array to a FUNCTION which calculates and prints the mean and median. The mean is the sum of the numbers divided by 11. The median is the midpoint value (half of the numbers are above it, and half are below it). To calculate the median, the array must be sorted. You may want to consider the quicksort function defined in chapter 9 of the course textbook. Save this as p1.c
  2. (5 Points) Write a program that reads an integer, N, from the user. The program then calls a function with the following function declaration N times.: void functionForProb2( ); This function will print the message “I've been called %d times\n”, where %d is the actual number of times the function has been called. The function can only use variables declared within the function. i.e. no global (external) variables and no parameters are sent to the function. Save this as p2.c
  3. (5 Points) Write a program which defines the following function. void get_rational(int *num, int *den) This function should read a rational number from the user. The rational number should be entered in the form “i/j” where i and j are integers. It should return the numerator(i) in num and the denominator in den. The main function will then print out both the numerator and the denominator. Save this as p3.c
  4. (5 Points) This part is not a programming assignment that will be compiled. I am asking you to trace the code manually. You could code, compile and run these programs to get the answer, but as you will not have a computer available to you on the exam, I would not suggest this. Put the answers to these in a text file called p4.txt. a) What will be the output to the following program. void swap(int a, int b);

int main(void) { int i = 1, j = 2; swap(i, j); printf("i = %d, j = %d\n", i, j); return 0; } void swap(int a, int b) { int temp = a; a = b; b = temp; } b) What will be the output to the following program. #include <stdio.h> #define N 15 #define FALSE 0 #define TRUE 1 int i; int eval(void); main() { i = N; i = eval(); if (!i) printf("FALSE\n"); else printf("TRUE\n"); return 0; } int eval(void) { int i = 0; i += N; return ((i/2) == N); } c) Whats the value of i after this code segment is executed? int i, *p; i = 5; p = &i; p = 10;