|
 |
|
 |
12-05-2005, 10:19 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
Charge account number
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 the number is valid. If the user enters a number that is not in the array the program should display a message indicating the number is invalid.
Charge Account Validation Modification
Modify the program so it performs a binary search to locate valid account numbers. Use the selection sort algorithm to sort the array before the binary search is performed.
That was the question and below is what I did as solution but can't seem to make it work and I'm looking for help as usual
Code:
#include <iomanip>
#include <cstring>
#include <fstream>
#include <utility>
#include <conio.h>
//******************************************************************************
//Symbolic Constants
#define ARRAY_SIZE 18
//******************************************************************************
// Function Prototypes
void Linear_Sort (int [], int);
void Selection_Sort (int [], int);
void Binary_Search (int [], int, int);
//******************************************************************************
int main ()
{
int NUM_ELS = 18;
int array [ARRAY_SIZE] = {5658845,8080152,1005231,4520125,4562555,6545231,7895122,5552012,3852085,8777541,5050552,7576651,8451277,7825877,7881200,1302850,1250255,4581002};
// Function calling.
Linear_Sort (array, NUM_ELS);
Selection_Sort (array, NUM_ELS);
//Binary_Search (int [], int, int);
cout << endl << endl << endl << "Press Any Key to Continue...";
getch ();
return 0;
//*****************************************************************************
void Linear_Sort (int array[], int NUM_ELS);
{
int charge_num;
int i;
bool found = false;
cout << " Enter account charge number " << " : "<< endl;
cin >> charge_num;
for(int i = 0; i < 18; i++)
{
if (array[i] == charge_num){
found = true;
break;
}
}
if (found){
cout << " Number is valid " << endl;
}
else{
cout << " Number is invalid " << endl;
}
}
//******************************************************************************
void Selection_Sort (int array[], int NUM_ELS);
{
int start_Scan;
int min_Index;
int min_Value;
for(start_Scan = 0; start_Scan < (NUM_ELS - 1); start_Scan++)
{
min_Index = start_Scan;
min_Value = array[start_Scan];
for (int index = start_Scan + 1; index < NUM_ELS; index++)
{
if (array[index] < min_Value)
{
min_Value = array[index];
min_Index = index;
}
}
array[min_Index] = array[start_Scan];
array[start_Scan] = min_Value;
}
}
}
//******************************************************************************
//void Binary_Search (int [], int, int);
//while(i < 18 && !found)
//{
//if (array [i] == charge_num )
//{
// found = true;
//}
// i++;
//}
// if (found)
// cout << " Number is valid " << endl;
//else
// cout << " Number is invalid " << endl;
// cout << " arraynum " << arraynum[0];
//cout << endl << endl << endl << "Press Any Key to Continue...";
//getch ();
//return 0;
//}
Thanks
Last edited by Valmont; 12-06-2005 at 01:29 AM.
|
|
|
12-06-2005, 01:49 AM
|
#2 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Quote:
...and I'm looking for help as usual
...
|
No problem. You did your homework and could use some assistance. Good attitude  .
I didn't check for correctness because officially I am at work now. However you need to be more accurate when you type.
Here is the first part with all the mistakes corrected:
Code:
#include <iomanip>
#include <cstring>
#include <fstream>
#include <utility>
#include <iostream>
using namespace std;
//Symbolic Constants
const int ARRAY_SIZE = 18;
// Function Prototypes
void Linear_Sort (int[], int);
void Selection_Sort (int [], int);
void Binary_Search (int [], int, int);
//************************************************** ***********************
int main ()
{
int NUM_ELS = 18;
int array [ARRAY_SIZE] = { 5658845, 8080152, 1005231, 4520125, 4562555,
6545231, 7895122, 5552012, 3852085, 8777541,
5050552, 7576651, 8451277, 7825877, 7881200,
1302850, 1250255, 4581002};
Linear_Sort (array, NUM_ELS);
//Selection_Sort (array, NUM_ELS);
//Binary_Search (int [], int, int);
return 0;
}
//************************************************** ************************
void Linear_Sort (int array[], int NUM_ELS)
{
int charge_num;
bool found = false;
cout << " Enter account charge number " << " : "<< endl;
cin >> charge_num;
for(int i = 0; i < NUM_ELS; i++)
{
if (array[i] == charge_num)
{
found = true;
break;
}
}
if (found)
{
cout << " Number is valid " << endl;
}
else
{
cout << " Number is invalid " << endl;
}
}
tips
- Try not to use "conio.h" and "getch()". Use :std::cin.get()" instead. In fact, it's even illegal in this part of the forum. That has a reason.
- Try not to use "#define" for constants. Use the "const" qualifier like I did. Otherwise we mix C and C++. Try not to do that when it's not required.
- Remove the:
Code:
if (found)
{
cout << " Number is valid " << endl;
}
else
{
cout << " Number is invalid " << endl;
}
This has nothing to do with Sorting or searching. It is just status message. Move it to its own function. As always, let functions do one thing and one thing only.
Okidoky, this should give you a fresh start  . Gotta go.
__________________
|
|
|
12-06-2005, 06:59 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
Thank you very much, it really helped to see where I was missing.
Thanks again
|
|
|
12-08-2005, 12:02 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
Aeroplane Seating
Sory to bother you guys again I had planned out the entire pseudo code but I lost the entire idea behind it after reaching point where I needed help (I guess), below is the question:
Write a C++ program to assign passengers seats in an airplane. Assume a small airplane with seat numbering as follows:
A B C D
A B C D
A B C D
A B C D
A B C D
A B C D
A B C D
Your program example, after should display the seat pattern with an ‘X’ marking an assigned seat. For seats 1A, 2B, and 4C are taken, the display should look like this:
1 X B C D
2 A X C D
3 A B C D
4 A B X D
5 A B C D
6 A B C D
7 A B C D
After displaying the seats available, the program prompts for the seat desired, the user types in a seat, and then the display of available seats is updated. This continues until all seats are filled or until the user signals that the program should end. If the user types in a seat that is already assigned, the program should say that that seat is occupied and ask for another choice.
Note: Your program should make use of separate functions to display the seat pattern, to clear (un-assign) all seats, and to mark a user-desired seat as assigned.
And this is what I did
//---------------------------------------------------------------------------
// Symbolic Constants
#define TOTALROWS 7
#define TOTALCOLS 4
//---------------------------------------------------------------------------
// Function Prototypes
bool SeatsRemain ( char [][TOTALCOLS], int);
void DisplaySeats ( char [][TOTALCOLS], int);
void EmptySeats ( char [][TOTALCOLS], int);
//---------------------------------------------------------------------------
int main (void)
{
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
char Plane[TOTALROWS][TOTALCOLS] = { 'X' };
int aisle;
char seat;
char position;
char request[3];
bool keepGoing;
EmptySeats( Plane, TOTALROWS);
keepGoing = true;
do{
DisplaySeats( Plane, TOTALROWS);
cout << "Enter desired seat (e.g. 2B): ";
gets(request);
aisle = request[0] - '0';
position = request[1];
seat = toupper(position);
if ( (aisle <= 0) || (aisle > TOTALROWS) ||
(seat < 'A') || (seat >= 'A' + TOTALCOLS)){
keepGoing = false; //validity check
for desired seat
cout << endl << endl << endl << "Press Any Key to Continue...";
getch ();
return 0;
//__________________________________________________ _____________
Let me know if you would like to see the pseudo code that I created.
Thanks
|
|
|
12-08-2005, 04:53 AM
|
#5 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
And what is anyone going to do with that?
For example, what does this mean:
Code:
char Plane[TOTALROWS][TOTALCOLS] = { 'X' };
Do all the X mean the seats are empty? So an 'O' means a seat is taken?
What exactly do you want from us?
__________________
|
|
|
12-08-2005, 06:33 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
yes, all the 'X' means the seat are taken and if it is still 'A' or 'B' or 'C' or 'D' then the seats are available.
|
|
|
12-08-2005, 07:18 AM
|
#7 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Using my code you could build your own around it:
Code:
#include <iostream>
#include <cstring>
using std::endl;
using std::cout;
//--
const unsigned ROWS = 7;
const unsigned COLS = 4;
static char seat[7][COLS+1] = {0}; //COLS+1 for terminating character.
//--
int main()
{
for(unsigned i = 0; i < ROWS; ++i)
{
std::strcpy(seat[i], "ABCD");
}
for(unsigned i = 0; i < ROWS; ++i)
{
cout<<seat[i]<<endl;
}
//Let's take seat at position [3,2]. Since indexes start at 0 we need to
//substract 1: position = [3-1][2-1].
seat[2][1] = 'X';
cout<<std::endl<<"After taking a seat:"<<endl<<endl;
for(unsigned i = 0; i < ROWS; ++i)
{
cout<<seat[i]<<endl;
}
return 0;
}
__________________
|
|
|
12-13-2005, 05:22 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
Here is what I did
Code:
// Symbolic Constants
#define ROWS 7
#define COLS 4
//---------------------------------------------------------------------------
char seat[7][COLS]; //COLS+1 for terminating character.
int main()
{
for(int i = 0; i < ROWS; ++i)
{
for(int j = 0; j < COLS; ++j)
{
seat[i][j]= j+'A';
}
}
for(int i = 0; i < ROWS; ++i)
{
cout<< (i+1);
for(int j = 0; j < COLS; ++j)
{
cout<< " " << seat[i][j];
}
cout<< endl;
}
//Let's take seat at position [3,2]. Since indexes start at 0 we need to
//substract 1: position = [3-1][2-1].
//Asking for user input
char aisle; //for rows
char seat; //for coulumns
cout<< "Please enter your desired seat no. (eg, 3A): ";
cin >> aisle >> seat;
int row = aisle -'0' - 1; //to calculate the row
int col = seat - 'A'; //to calculate the column
seat[3][2] = 'X';
cout<<endl<<"After taking a seat:"<<endl<<endl;
for(int i = 0; i < ROWS; ++i)
{
for(int j = 0; j < COLS; ++j)
{
cout<<seat[i][j];
}
cout<<endl;
}
cout << endl << endl << endl << "Press Any Key to Continue...";
getch ();
return 0;
}
But I get a error "Invalid Indirection" at seat[3][2] = 'X';
and another one is same "Invalid Indirection" at cout<<seat[i][j];
So please let me know what I'm doing wrong.
Thanks
Last edited by Valmont; 12-13-2005 at 09:41 PM.
|
|
|
12-13-2005, 09:48 PM
|
#9 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
I'll back in a moment but first I want you to trade your #define statements by const or static const like I did.
Secondly I don't want to see getchar() anymore. Change it and see my code how I defined the constants.
__________________
|
|
|
12-13-2005, 10:13 PM
|
#10 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
Spot the differences. Especially how you had two variables called "seat". The second one is now called: Seat (starts with a capital letter).
Code:
#include <iostream>
#include <cstring>
using std::endl;
using std::cout;
using std::cin;
//--
static const unsigned ROWS = 7;
static const unsigned COLS = 4;
//---------------------------------------------------------------------------
char seat[7][COLS];
int main()
{
for(unsigned i = 0; i < ROWS; ++i)
{
for(unsigned j = 0; j < COLS; ++j)
{
seat[i][j]= j+'A';
}
}
for(unsigned i = 0; i < ROWS; ++i)
{
cout<< (i+1);
for(unsigned j = 0; j < COLS; ++j)
{
cout<< " " << seat[i][j];
}
cout<< endl;
}
//Let's take seat at position [3,2]. Since indexes start at 0 we need to
//substract 1: position = [3-1][2-1].
char Aisle; //for rows
char Seat; //for coulumns
cout<< "Please enter your desired seat no. (eg, 3A): ";
cin >> Aisle >> Seat;
unsigned row = Aisle -'0' - 1; //to calculate the row
unsigned col = Seat - 'A'; //to calculate the column
seat[row][col] = 'x';
cout<<endl<<"After taking a seat:"<<endl<<endl;
for(unsigned i = 0; i < ROWS; ++i)
{
for(unsigned j = 0; j < COLS; ++j)
{
cout<<seat[i][j];
}
cout<<endl;
}
//Remove the comment slashes below if your console "flashes away":
//std::cin.get();
return 0;
}
__________________
|
|
|
12-14-2005, 06:40 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 24
|
Thank you very much you are great!!
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 09:32 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|