CSC 2100: Introduction to Problem Solving and Computer Programming

Fall 2008, Test 2, November 25, 2008

Time: 55 minutes 100 Points

Name ______

  1. [5 Points] Given the following code in main, write an appropriate prototype for the function doSomething( ).

int a, b, d;

double c;

………

d = doSomething(a, b, c)

int doSomething(int, int, double);

  1. [20 Points] Write a function that accepts an array of integers and number of element in the arrays as arguments. The function then computes two sums from the elements. One is the sum of all elements that are odd and other is the sum all elements that are even. The function then displays the two sums.

void oddEvenSum(int a[], int n)

{

int i, evenSum = 0, oddSum = 0;

for (i = 0; i < n; i++)

{

if (a[i] % 2 == 0)

evenSum = evenSum + a[i];

else

oddSum = oddSum + a[i];

}

cout < “Sum of the even integer is < evenSum < endl;

cout < “Sum of the even integer is < evenSum < endl;

}

  1. [25 Points] Write a function that finds the maximum and minimum elements of a two dimensional array and store them in argument max and min. The function header is as follows

void minMax(double a[][5], int row, double &max, double &min) /* row is the number of rows*/

{

int i, j;

max = a[0][0];

mix = a[0][0];

for (i = 1; i < row; i++)

{

For (j = 1; j < 5; j++)

{

if (a[i][j] > max)

max = a[i][j];

if (a[i][j] < min)

min = a[i][j];

}

}

}

  1. [20 Points]Write a function that accepts a pointer to a C-string as an argument and displays it content backward. For example if the string argument is “CSC2100” the function should display “0012CSC”. Demonstrate the function in a program that asks the user to input a string and then passes it to the function.

#include <ctring>

void reverse (char *c)

{

int len, I;

len = strlen(c);

for (i = len - 1; i >= 0 I --)

cout < c[i];

}

main()

{

char s[80];

cout < “ Enter a string : “;

cin.getline(s,80);

reverse(s);

}

  1. [30 Points] Write a program that allows the user to enter last name of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name, the number of vote received, and the percentage of total vote received by the candidate [15 Points]. Your program should also output the winner of the election [5 Points]. You must use structure [5 Points] to store information about a candidate and use pointer to access the structure element [5 Points]. A sample output is:

CandidateVotes received%Total Votes

Eric4020%

Cody5025%

Tyler1608%

Paul9045%

Ghafoor0402%

The winner of the election is Paul

struct Candidate

{

char lname[16];

int vote;

double percent;

};

main()

{

int i, winner = 0, max;

double total = 0;

candidate c[5];

for (i = 0; i < 5; i ++)

{

cout < Enter Candidate’s last name: “;

cin > (c+i)->lname;

cout < Enter how many vote Candidate got : “;

cin > (c+i)->vote;

total = total + (c+i)->vote;

}

max = c[0].vote;

for (i = 0; i < 5; i ++)

{

if((c+i)->vote > max;

{

max = (c+i)->.vote;

winner = i;

}

(c+i)->percent = ((c+i)->vote/total)*100;

}

cout < “ CandidateVote Percentage” < endl;

for (i = 0; i < 5; i ++)

{

cout < setw[20] < (c+i)->name < setw[10] < (c+i)->vote < setw(10) < (c+i)->percent;

}

cout < “The winner is “< (c+winner).lname;

}