Chapter 9 Median Function

/*9. Median Function
In statistics, when a set of values is sorted in ascending or descending order, its median is  the  middle value.  If  the  set  contains  an even  number  of  values, the median is the mean, or average, of the two middle values. Write a function that accepts as arguments the following:
A) An array of integers
B) An integer that indicates the number of elements in the array
The function should determine the median of the array. This value should be returned as a double. (Assume the values in the array are already sorted.) Demonstrate  your  pointer  prowess  by  using  pointer  notation  instead  of  array  nota-tion in this function.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
double getMedian(int*,int);

int main()
{
int size;
cout<<“Enter the number of numbers: “;
cin>>size;
int* numberList=new int[size];
for(int i=0;i<size;i++)
{
cout<<“\nEnter the “<<i+1<<” number: “;
cin>>numberList[i];
}

double median=getMedian(numberList,size);
cout<<median<<endl;
system(“pause”);
return 0;
}
double getMedian(int* numbers,int size)
{

//sort number
int minValue,minIndex;
for(int startScan=0;startScan<size-1;startScan++)
{
minValue=numbers[startScan];
minIndex=startScan;
for(int index=startScan+1;index<size;index++)
{
if(numbers[startScan]>numbers[index])
{
minValue=numbers[index];
minIndex=index;
}

}
numbers[minIndex]=numbers[startScan];
numbers[startScan]=minValue;
}

//get median
double median;
if(size%2==0) median=(numbers[size/2]+numbers[size/2-1])/2.0;
else median=numbers[(size-1)/2];
return median;
}

Advertisements

Chapter 9 Mode Function

/*8. Mode Function
In statistics, the mode of a set of values is the value that occurs most often or with the greatest frequency. Write a function that accepts as arguments the following:
A) An array of integers
B) An integer that indicates the number of elements in the array
The  function  should  determine  the  mode  of  the  array.  That  is,  it  should  determine which value in the array occurs most often. The mode is the value the function should return.  If  the  array  has  no  mode  (none  of  the  values  occur  more  than  once),  the function should return -1. (Assume the array will always contain nonnegative values.) Demonstrate your pointer prowess by using pointer notation instead of array notation in this function.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
int mode(int* ,int );

int main()
{
int * number,size;
cout<<“\nEmter the number of integers: “;
cin>>size;
number=new int[size];

for(int i=0;i<size;i++)
{
cout<<“\nEnter the “<<i+1<<” number: “;
cin>>number[i];
}

int theMode=mode(number,size);

if(theMode==-1) cout<<“\nThere is no mode of these numbers.”<<endl;
else cout<<“\nThe mode of these numbers is “<<theMode<<endl;
system(“pause”);
return 0;
}

int mode(int* number,int size)
{
int* frequency=new int[size];

for(int i=0;i<size;i++)
{
frequency[i]=0;
for(int x=0;x<size;x++)
if(number[i]==number[x])frequency[i]++;
}

int maxF=frequency[0];
int mode;

for(int y=1;y<size;y++)
{
if(frequency[y]>=maxF)
{
maxF=frequency[y];
mode=number[y];

}
}

if(mode==0) mode=-1;
return mode;
}

 

Chapter 9 Test Score#2

/*4. Test Scores #2
Modify the program of Programming Challenge 2 to allow the user to enter name-score pairs. For each student taking a test, the user types the students name followed by the students integer test score. Modify the sorting function so it takes an array holding the student  names  and  an  array  holding  the  student  test  scores.  When  the  sorted  list  of scores is displayed, each students name should be displayed along with his or her score. In stepping through the arrays, use pointers rather than array subscripts.*/

#include “stdafx.h”
#include<iostream>
#include<string>

using namespace std;
void sortScore(int *,int,string* );

int main()
{
int numberOfStudents;
cout<<“Enter the number of students: “;
cin>>numberOfStudents;

int* score=new int[numberOfStudents];
string* nameOfStudent=new string[numberOfStudents];

for(int i=0;i<numberOfStudents;i++)
{
cout<<“\nEnter the name of “<<i+1<<” student: “;
cin>>nameOfStudent[i];
cout<<“\nEnter the score of the student: “;
cin>>score[i];
}

sortScore(score, numberOfStudents,nameOfStudent);

cout<<“\nList of Scores in ascending order”;
for(int i=0;i<numberOfStudents;i++)
{
cout<<“\n”<<nameOfStudent[i]<<“\t\t”<<score[i];
}

cout<<endl;

delete [] score;
score=0;

system(“pause”);
return 0;
}

void sortScore(int *score,int size,string* name)
{
int startScan,minIndex;
int minValue;
string minName;

for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=score[startScan];

for(int index=startScan+1;index<size;index++)
{
if(score[index]<minValue)
{
minValue=score[index];
minIndex=index;
minName=name[index];
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
name[minIndex]=name[startScan];
name[startScan]=minName;
}

}

Chapter 9 Drop Lowest Score

/*3. Drop Lowest Score
Modify Problem 2 above so the lowest test score is dropped. This score should not be included in the calculation of the average.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
void sortScore(int *,int );
double getAverage(int *,int);

int main()
{
int numberOfScore;
cout<<“Enter the number of test scores: “;
cin>>numberOfScore;

int* score;
score= new int [numberOfScore];

for(int i=0;i<numberOfScore;i++)
{
cout<<“\nEnter the score for “<<i+1<<” test: “;
cin>>score[i];
}

sortScore(score, numberOfScore);

cout<<“\nAfter dropping the lowest score,List of Scores in ascending order”;
for(int i=1;i<numberOfScore;i++)
{
cout<<“\n”<<score[i];
}
double average=getAverage(score,numberOfScore);

cout<<“\nThe average of scores is “<<average<<endl;

delete [] score;
score=0;

system(“pause”);
return 0;
}

void sortScore(int *score,int size)
{
int startScan,minIndex;
int minValue;

for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=score[startScan];

for(int index=startScan+1;index<size;index++)
{
if(score[index]<minValue)
{
minValue=score[index];
minIndex=index;
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
}

}

double getAverage(int * score,int size)
{
double sum=0.0,avg;
for(int i=1;i<size;i++)
{
sum+=score[i];
}
return avg=sum/(size-1);
}

Chapter 9 Test Score#1

/*2. Test Scores #1
Write  a  program  that  dynamically  allocates  an  array  large  enough  to  hold  a  user-defined  number  of  test  scores.  Once  all  the  scores are  entered,  the  array  should  be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings. Use pointer notation rather than array notation whenever possible.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
void sortScore(int *,int );
double getAverage(int *,int);

int main()
{
int numberOfScore;
cout<<“Enter the number of test scores: “;
cin>>numberOfScore;

int* score;
score= new int [numberOfScore];

for(int i=0;i<numberOfScore;i++)
{
cout<<“\nEnter the score for “<<i+1<<” test: “;
cin>>score[i];
}

sortScore(score, numberOfScore);

cout<<“\nList of Scores in ascending order”;
for(int i=0;i<numberOfScore;i++)
{
cout<<“\n”<<score[i];
}
double average=getAverage(score,numberOfScore);

cout<<“\nThe average of scores is “<<average<<endl;

delete [] score;
score=0;

system(“pause”);
return 0;
}

void sortScore(int *score,int size)
{
int startScan,minIndex;
int minValue;

for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=score[startScan];

for(int index=startScan+1;index<size;index++)
{
if(score[index]<minValue)
{
minValue=score[index];
minIndex=index;
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
}

}

double getAverage(int * score,int size)
{
double sum=0.0,avg;
for(int i=0;i<size;i++)
{
sum+=score[i];
}
return avg=sum/size;
}

Chapter 8 Charge Account Validation Modification

/*4. Charge Account Validation Modification
Modify the program you wrote for Problem 1 (Charge Account Validation) so it per-forms  a  binary  search  to  locate  valid  account  numbers.  Use  the  selection  sort  algo-rithm to sort the array before the binary search is performed.*/

#include “stdafx.h”
#include<iostream>
#include<string>

using namespace std;
void sort(int [],int);
bool validation(int [],int,int);

int main()
{
//input list
const int size=18;
int list[size]={5658845,4520125,7895122,8777541,8451277,1302850,8080152,4562555,5552012,
5050552,7825877,1250255,1005231,6545231,3852085,7576651,7881200,4581002};

//get user number
int userNumber;
cout<<“Enter your account number: “;
cin>>userNumber;

//stor account
sort(list,size);

//check number
if(validation(list,size,userNumber)) cout<<“The number is valid.”<<endl;
else cout<<“The number is invalid.”<<endl;

system(“pause”);
return 0;
}

void sort(int accountList[],int size)
{
int startScan, minIndex,minValue;

for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=accountList[startScan];
for(int index=startScan+1;index<size;index++)
{
if(accountList[index]<minValue)
{
minValue=accountList[index];
minIndex=index;
}
accountList[minIndex]=accountList[startScan];
accountList[startScan]=minValue;
}
}
}

bool validation(int list[],int size,int userN)
{
bool validation=false;
int first=0,
last=size-1,
middle;

while(!validation&&first<=last)
{
middle=(first+last)/2;
if(list[middle]==userN) validation=true;
else if(list[middle]>userN) last=middle=-1;
else first=middle+1;
}

return validation;
}

Chapter 8 Lottery Winners Modification

/*

3. Lottery Winners Modification
Modify the program you wrote for Programming Challenge 2 (Lottery Winners) so it
performs a binary search instead of a linear search.

*/

#include “stdafx.h”
#include<iostream>
#include<vector>

using namespace std;
void ticketSort(vector<int>&);
bool search(vector<int>,int);

int main()
{
vector<int> lotteryList;

//Store values in the vector.
lotteryList.push_back(13579);
lotteryList.push_back(26791);
lotteryList.push_back(26792);
lotteryList.push_back(33445);
lotteryList.push_back(55555);
lotteryList.push_back(62483);
lotteryList.push_back(77777);
lotteryList.push_back(79422);
lotteryList.push_back(85647);
lotteryList.push_back(93121);

//get winning number
int winningNumber;

cout<<“Enter this week’s winning number “;
cin>>winningNumber;

//sort ticket
ticketSort(lotteryList);

//check if win
if(search(lotteryList,winningNumber)) cout<<“One of the tickets is a winner this week.”<<endl;
else cout<<“Not one of the tickets is a winner this week.”<<endl;

system(“pause”);
return 0;
}

void ticketSort(vector<int>& ticket)
{

int size=ticket.size();
int startScan,minIndex,tempTicket,minTicket;
for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minTicket=ticket[startScan];
for(int index = startScan+1;index<size;index++)
{
if(minTicket>ticket[index])
{
minTicket=ticket[index];
minIndex=index;
}
ticket[minIndex]=ticket[startScan];
ticket[startScan]=minTicket;
}
}

}

bool search(vector<int> list,int winNumber)
{
bool found=false;
int first=0,
last=list.size()-1,
middle;
while(!found&&first<=last)
{
middle=(first+last)/2;
if(list[middle]==winNumber) found=true;
else if(list[middle]>winNumber) last=middle-1;
else first=middle+1;
}
return found;
}

Chapter 8 Lottery Winners

/*

2. Lottery Winners
A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit lucky   combinations.  Write  a  program  that  initializes  an  array  or  a vector  with these numbers and then lets the player enter this weeks winning 5-digit number. The program should perform a linear search through the list of the players numbers and report whether or not one of the tickets is a winner this week. Here are the numbers:
13579  26791  26792  33445  55555
62483  77777  79422  85647  93121

*/

#include “stdafx.h”
#include<iostream>
#include<vector>

using namespace std;

int main()
{
vector<int> lotteryList;

//Store values in the vector.
lotteryList.push_back(13579);
lotteryList.push_back(26791);
lotteryList.push_back(26792);
lotteryList.push_back(33445);
lotteryList.push_back(55555);
lotteryList.push_back(62483);
lotteryList.push_back(77777);
lotteryList.push_back(79422);
lotteryList.push_back(85647);
lotteryList.push_back(93121);

//get winning number
int winningNumber;

cout<<“Enter this week’s winning number “;
cin>>winningNumber;

for(int i=0;i<10;i++)
{
if(lotteryList[i]==winningNumber)
{
cout<<“One of the tickets is a winner this week.”<<endl;

break;
}else if(i==9) cout<<“Not one of the tickets is a winner this week.”<<endl;
}

system(“pause”);
return 0;
}

Chapter 8 Charge Account Validation

/*1. Charge Account Validation
Write  a  program  that  lets  the  user  enter  a  charge  account  number.  The  program
should determine if the number is valid by checking for it in the following list:
5658845   4520125   7895122   8777541   8451277   1302850
8080152   4562555   5552012   5050552   7825877   1250255
1005231   6545231   3852085   7576651   7881200   4581002
The list of numbers above should be initialized in a single-dimensional array. A simple
linear  search  should  be  used  to  locate  the  number  entered  by  the  user.  If  the  user
enters a number that is in the array, the program should display a message saying that
the number is valid. If the user enters a number that is not in the array, the program
should display a message indicating that the number is invalid.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
bool validation(int[] ,int,int);

int main()
{
//input list
const int size=18;
int list[size]={5658845,4520125,7895122,8777541,8451277,1302850,8080152,4562555,5552012,
5050552,7825877,1250255,1005231,6545231,3852085,7576651,7881200,4581002};

//get user number
int userNumber;
cout<<“Enter your account number: “;
cin>>userNumber;

//check number

if(validation(list,size,userNumber)==true) cout<<“The number is valid.”<<endl;
else cout<<“The number is invalid.”<<endl;

system(“pause”);
return 0;
}

bool validation(int list[],int size,int userN)
{
bool validation=false;
for(int i=0;i<size;i++)
{
if(list[i]==userN)
{
validation=true;
return validation;
}

}
return validation;
}

Chapter 7 Tic-Tac-Toe Game

/*

15. Tic-Tac-Toe Game
Write  a  program  that  allows  two  players  to  play  a  game  of  tic-tac-toe.  Use  a  two-dimensional c ha rarray  with  three  rows  and  three  columns  as  the  game  board.  Each element of the array should be initialized with an asterisk (*). The program should run a
loop that Displays the contents of the board array Allows player 1 to select a location on the board for an X. The program should ask the user to enter the row and column number. Allows player 2 to select a location on the board for an O. The program should ask the user to enter the row and column number. Determines whether a player has won, or a tie has occurred. If a player has won, the program should declare that player the winner and end. If a tie has occurred, the program should say so and end. Player 1 wins when there are three Xs in a row on the game board. The Xs can appear in  a  row,  in  a  column,  or  diagonally  across  the  board.  A  tie  occurs  when  all  of  the locations on the board are full, but there is no winner.*/

#include “stdafx.h”
#include<iostream>

using namespace std;
bool player1Won();
bool player1WonInARow();
bool player1WonInAColumn();
bool player1WonInDia();

bool player2Won();
bool player2WonInARow();
bool player2WonInAColumn();
bool player2WonInDia();

char tic_tac_toe[3][3]={‘*’,’*’,’*’,’*’,’*’,’*’,’*’,’*’,’*’};
int player1R[5];
int player1C[5];
int player2R[4];
int player2C[4];
int countForP1R=0;
int countForP2R=0;

int main()
{
int i=0;
for(;i<9;i++)
{
int x,y,z,k;
//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<“\t”;
}
cout<<endl;
}

//ask player one to select a location
cout<<“\nPLAYER1\nSelect a location by entering two number while”
<<“\nthe first number, in the range of 0 to 2, stands”
<<“\nfor the row of the loation and the second num, ”
<<“\n in the range of 0 to 2, stands for the columns”
<<“\nof the location. Sperate two number by space.”
<<“\nONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE”
<<“\n”;
cin>>x>>y;
tic_tac_toe[x][y]=’X’;
player1R[i]=x;
player1C[i]=y;

//display the contents of the board
for(int a=0;a<3;a++)
{
for(int b=0;b<3;b++)
{
cout<<tic_tac_toe[a][b]<<“\t”;
}
cout<<endl;
}
//determinate if player1 wins
if(player1Won()==true)
{
cout<<“Player 1 wins the game!”;
system(“pause”);
return 0;
}

//ask player two to select a location
cout<<“\nPLAYER2\nSelect a location by entering two number while”
<<“\nthe first number, in the range of 0 to 2, stands”
<<“\nfor the row of the loation and the second num, ”
<<“\n in the range of 0 to 2, stands for the columns”
<<“\nof the location. Sperate two number by space.”
<<“\nONLY LOCATIONS OCCUPIED BY * ARE AVALIABLE”
<<“\n”;
cin>>z>>k;
tic_tac_toe[z][k]=’O’;
player2R[i]=z;
player2C[i]=k;

//determinate if player2 wins
if(player2Won()==true)
{
cout<<“Player 2 wins the game!”;
system(“pause”);
return 0;
}
}

system(“pause”);
return 0;

}

///////////////////////////////////////////////////////
////////////determinate if player1 wins////////////////
///////////////////////////////////////////////////////

bool player1Won()
{
bool player1Won=false;
if(player1WonInARow()==true)
{
cout<<“player1WonInARow”;
player1Won=true;
}
if(player1WonInAColumn()==true)
{
cout<<“player1WonInAColumn”;
player1Won=true;
}
if(player1WonInDia()==true)
{
cout<<“player1WonInDia”;
player1Won=true;
}

return player1Won;
}
bool player1WonInARow()
{
bool player1WonInARow=false;
if(tic_tac_toe[0][0]==’X’&&tic_tac_toe[1][0]==’X’&&tic_tac_toe[2][0]==’X’) player1WonInARow=true;
else if(tic_tac_toe[0][1]==’X’&&tic_tac_toe[1][1]==’X’&&tic_tac_toe[2][1]==’X’) player1WonInARow=true;
else if(tic_tac_toe[0][2]==’X’&&tic_tac_toe[1][2]==’X’&&tic_tac_toe[2][2]==’X’) player1WonInARow=true;

return player1WonInARow;
}
bool player1WonInAColumn()
{
bool player1WonInAColumn=false;

if(tic_tac_toe[0][0]==’X’&&tic_tac_toe[0][1]==’X’&&tic_tac_toe[0][2]==’X’) player1WonInAColumn=true;
else if(tic_tac_toe[1][0]==’X’&&tic_tac_toe[1][1]==’X’&&tic_tac_toe[1][2]==’X’) player1WonInAColumn=true;
else if(tic_tac_toe[2][0]==’X’&&tic_tac_toe[2][1]==’X’&&tic_tac_toe[2][2]==’X’) player1WonInAColumn=true;
return player1WonInAColumn;

}