# [30]Chapter10 String Length

/*1.String Length
Write a function that returns an integer and accepts a pointer to a C-string as an argu-ment.  The  function  should  count  the  number  of  characters  in  the  string  and  return
that number. Demonstrate the function in a simple program that asks the user to input
a string, passes it to the function, and then displays the functions return value.*/

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

using namespace std;

int stringCount(string);

int main()
{
string string;
cout<<“\nEnter string “;
cin>>string;
int count;
count=stringCount(string);

cout<<“\nThe number of characters in the string is “<<count<<” .\n”;

system(“pause”);
return 0;
}

int stringCount(string str)
{
int i=0;
int count=0;

while(str[i]!=”)
{
count++;
i++;
}
return count;
}

# 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;
}

# 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;
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;
}