So I've run into a problem while writing this simple line of code for a school project. What happens is that after asking for user input to fill an array with values, the input for the last column overwrites all the other columns. I've tried finding the problem myself but I just can't seem to find it!
Part of the code that I think is problematic:
for (col = 0; col < arraywidth; col++) {
for (row = 0; row < arrayheight; row++) {
cout << "Please input a value for element " << col << ", " << row << "." << endl;
cin >> T[col][row];
}
}
Thanks for your help. Here's the full code:
#include <iostream>
using namespace std;
int main()
{
// ------------- Variables ---------------------------------
int arrayheight, arraywidth, col, row, maxe, pos_c, T[arrayheight][arraywidth];
// ---------------------------------------------------------
// ------------- Array Creation & Filling ------------------
cout << "Please insert the height of the array." << endl;
cin >> arrayheight;
cout << "Please insert the width of the array." << endl;
cin >> arraywidth;
cout << "Now we will be inputting values in the two-dimensional array." << endl;
for (col = 0; col < arraywidth; col++) {
for (row = 0; row < arrayheight; row++) {
cout << "Please input a value for the element " << col << ", " << row << "." << endl;
cin >> T[col][row];
}
}
// ---------------------------------------------------------
// ------------- Displaying the Array ----------------------
cout << "Now we will be displaying the array." << endl;
for (col = 0; col < arraywidth; col++) {
cout << endl;
for (roaw = 0; row < arrayheight; row++) {
cout << T[col][row] << " | ";
}
}
// ---------------------------------------------------------
// ------------- Logical Function as Requested -------------
cout << " " << endl;
for (col = 0; col < arraywidth; col++) {
maxe = -99;
for (row = 0; row < arrayheight; row++) {
if (T[col][row] > maxe) {
maxe = T[col][row];
pos_c = col;
}
}
cout << "The maximum value of the column " << col << " " << "is " << maxe << endl;
}
// ---------------------------------------------------------
return 0;
}
Picture of what happens:
You have the declaration of the array t[arrayheight][arraywidth] before you assign to those variables. You need to move that declaration down to after the variables are read from the user.
Also, C++ doesn't allow variable-length arrays -- that's a G++ extension. You should use std::vector<std::vector<int>> or dynamic allocation with new.
Related
So I made this program in C++ in which the user is prompted to enter a number and a multiplication table is generated based on the number that is inputted. It will continuously ask the user to input a number until an alphabet letter is pressed to quit.
I'm trying to make the table go up to 10, as it only goes up to 9. When you try to enter 10, it only generates a 1 x 1 table then the program closes. Also, I'm trying to make it so that when you enter a negative number such as -3, it displays something like "Number must be greater than 0" then the program continues to ask for a number input.
Here is my code so far
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
cout << "Please enter a number (press q or any letter to quit) ";
while(cin>>userSelection)
{
int numForTable = (int)userSelection - 48;
if(numForTable<1 or numForTable > 12)
{
cout << "" << endl;
cout<<"Have a nice day"<<endl;
return 0;
}
else
{
cout << "\n"
<< "Multiplication table for: " << numForTable << " is:" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
cout << "" << endl;
cout << "Please enter a number (press q or any letter to quit) ";
}
return 0;
}
You're reading a single character:
while(cin>>userSelection)
And then converting it to its numeric value:
int numForTable = (int)userSelection - 48;
The behaviour you're looking for would be better served by"
cin >> numForTable;
And choosing a different sentinel. Alternatively, read a full line of input and then extract the number from that string after checking for the q.
I made a program that generates a multiplication table for the number that is inputted by the user:
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
while (userSelection != 'q');
return 0;
}
It continuously asks the user to input a number until the program is closed, but I'm trying to make it so the program closes when the user inputs any alphabet letter followed by a message that says something like "Have a nice day"
I totally used your program to write the table, and only make the mechanism to switch between writing the table or exiting the program. Since your program only writes the table for integer between 1-10, which is 2-9, I used the ASCII code of the char for it. Here's the code.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
while(cin>>userSelection)
{
int numForTable = (int)userSelection - 48;
if(numForTable<2 or numForTable > 9) //because the limit of the table is between 1-10, that is 2-9
{
cout<<"Have a nice day"<<endl;
return 0;
}
else
{
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
}
return 0;
}
First, it changes the char input to the ASCII code, then it checks whether the number ranges from 2 to 9, then execute the program according to it. Please do correct me if i have any mistake on it.
You can type and input a character or string in 'numForTable' to quit the program but it will throw an error because of a type mismatch.
To handle this, you can use try and catch in c++
//start of do while loop
cout << "Please enter a number for your multiplication table: " << endl;
try{
cin >> numForTable;
if(!numForTable){ //if numForTable is not int, it will throw the the catch will be executed
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
// I do the same here
try{
cin >> numForTable;
if(!numForTable){
throw 0;
}
}catch(...){
cout<< "Have a nice day";
return 0;
}
}
I tried this and it worked
Also I changed the do while to -- do...while(true)
I've been trying to overcome this problem for a few hours now and I seem to have one approach to the situation. It seems that the use of selection statements worked in creating the table necessary. Although there are formatting issues.
I'd like to know if there was a way to create the same table
using only nested for-loops as mentioned by our professor.
Are the selection statements necessary or can we implement a system of nested for loops to acquire the same results?
The image below is the required table:
But the image below is what I have:
Below is my code:
for (int i = 0; i <= numChoice; ++i)
{
if (i == 0)
{
for (int k = 1; k <= numChoice; ++k)
{
cout << " " << k;
}
cout << "\n";
}
else
{
cout << i << " | ";
for (int j = 1; j <= numChoice; ++j)
{
if (j*i <= 9)
{
cout << " " << j*i << "|";
}
else if (j*i > 9 && j*i <= 100)
{
cout << " " << j*i << "|";
}
else if (j*i > 99 && j*i <= 999)
{
cout << " " << j*i << "|";
}
}
cout << "\n";
for (int k = 0; k <= numChoice; ++k)
{
if (k == 0)
{
cout << "-|";
}
else
{
cout << "----|";
}
}
cout << "\n";
}
}
The following code uses no if else constructs. The formatting can be got by using setw, used for setting the width of integers.Following code produces perfect output.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j;
cout<<" "<<1;//5 space chars
for(i = 2;i <= 10;++i)
cout<<" "<<i;//4 space chars
cout<<endl;
cout<<" ----|";
for(i = 2;i <= 10;++i)
cout<<"----|";
cout<<endl;
for(i = 1;i <= 10;++i)
{
cout<<setw(2)<<i<<"|";
for(j = 1;j <= 10;++j)
cout<<setw(4)<<j*i<<"|";
cout<<endl;
cout<<" -|----";
for(j = 2;j <= 9;++j)
cout<<"|----";
cout<<"|----|";
cout<<endl;
}
return 0;
}
#FranticCode. I'm also in the same class as you and was having problems with this homework assignment as well. I still don't understand it, but I figured out how to manipulate Sumeet's code to give us correct format. The ONLY thing I am having a problem with now is adding an empty space AFTER the first multiplication table and before the menu redisplay. I'll share what I have and maybe you can figure it out. Still going to ask the professor to review chapter 5 because I would like to learn it rather than just submit the homework.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
char userSelection;
int numForTable;
int col;
int row;
do
{
cout << "MENU" << endl
<< "a) Generate Multiplication Table" << endl
<< "q) Quit the program" << endl
<< "Please make a selection: ";
cin >> userSelection;
if (userSelection == 'a')
{
cout << "Please enter a number for your multiplication table: " << endl;
cin >> numForTable;
while (numForTable < 1 || numForTable > 10)
{
cout << "Please enter a number between 1 & 10." << endl;
cin >> numForTable;
}
cout << "\n"
<< "MULTIPLICATION TABLE: " << numForTable << "'s" << endl
<< "\n"
<< " " << 1;
for (col = 2; col <= numForTable; ++col)
cout << " " << col;
cout << endl;
cout << " ----|";
for (col = 2; col <= numForTable; ++col)
cout << "----|";
cout << endl;
for (col = 1; col <= numForTable; ++col)
{
cout << setw(2) << col << "|";
for (row = 1; row <= numForTable; ++row)
cout << setw(4) << col * row << "|";
cout << endl;
cout << " -|----";
for (row = 2; row <= numForTable - 1; ++row)
cout << "|----";
cout << "|----|";
cout << endl;
}
}
else if (userSelection != 'q')
{
cout << "Invalid Selection\n" << endl;
}
else if (userSelection == 'q')
{
cout << " You have chosen to quit the program. Thank you for using!" << endl;
}
}
while (userSelection != 'q');
//system("PAUSE");
return 0;
}
got curious to see if i could add the lines as easy as i claimed, it took a bit of fiddling, but here's the result (updated code below to also have lines).
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
int counter;
int counter2;
int amount;
cout << " |-----------------------------------------------------------|" << endl; // first line of table.
for(counter=1;counter<11;counter++){ // the 2 for lines create our 2 dimensional table
for(counter2=1;counter2<11;counter2++){
cout << " | " << setw(3) << counter*counter2; // setw(3) is a function of <iomanip>,
//setting minimum width to 3 for numbers.
}
cout << " |" << endl; // this here is being added to the end of each line and starts a new line.
cout << " |-----------------------------------------------------------|" << endl; // this is being inserted between each line, and starts a new line.
}
return 0;
}
Use the following construct:
for (int i=0; i<=numChoice; i++) // display first row of numbers
cout <<"\t" << i << "\t";
cout << "\n";
for (int i=0; i <=numChoice; i++) {
cout << i << "\t";
for (int j=0; j <=numChoice; j++)
cout << i*j << "\t";
cout << "\n";
}
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[]) {
ofstream outData;
ifstream inData;
string inString;
int matrix[12][12];
int rowSize, colSize;
inData.open("C:\\Users\\JSU\\Documents\\ArrayInput.txt");
inData >> rowSize >> colSize;
cout << "Row= " << rowSize << "\t Col= "<<colSize<< endl;
for (int r=0; r <rowSize; r++){
for (int c=0; c <colSize; c++){
inData >> matrix[r][c] ;
}
}
// print the input values
cout << "For the "<< rowSize << " x " << colSize << " array:"<<endl;
for (int r=0; r <rowSize; r++){
for (int c=0; c <colSize; c++){
cout << matrix[r][c] << " ";
}
cout<<endl;
}
cout << endl<<endl;
// find the largest in each row
for (int c=0; c <colSize; c++){
int largest = matrix[0][c]; //make the first cell value as the largest until you find otherwise
int smallest = matrix[0][c]; //make the first cell value as the smallest until you find otherwise
int sum=0;
for (int r=0; r <rowSize; r++){
if (matrix[r][c] > largest) //found a new larger value than the largest
largest = matrix[r][c];
if (matrix[r][c] < smallest) //found a new smaller value than the smallest
smallest = matrix[r][c];
sum = sum + matrix[r][c];
}
cout << "The sum of column "<< c + 1 << " is " << sum << endl;
cout << "The average of column "<< c + 1 << " is "<<fixed<<setprecision(2)<< (double)sum/rowSize << endl;
cout << "The largest value in column "<< c + 1 << " is " << largest << endl;
cout << "The smallest value in column "<< c + 1 << " is " << smallest << endl;
cout << "Is column " << c + 1 << " strictly ascending? " << endl;
cout<<endl;
}
inData.close();
return 0;
}
This is my code, and I've done almost everything of what I need. I just can't figure out how to cout the four corners of the array, and I need to print something that says cout << "Is column " << c + 1 << " strictly ascending? " << endl; and I can't figure out how to make that work.
Any and all help is appreciated.
Printing Matrix Corners
Here is how to print the four corners of a matrix.
Note that the corners are at: (0,0), (0, MAX_COLUMNS), (MAX_ROWS, 0) and (MAX_ROWS, MAX_COLUMNS).
const unsigned int MAX_ROWS = 12U;
const unsigned int MAX_COLS = 12U;
unsigned int matrix[MAX_ROWS][MAX_COLS];
//...
cout << matrix[0][0] << endl;
cout << matrix[MAX_ROWS][0] << endl;
cout << matrix[0][MAX_COLS] << endl;
cout << matrix[MAX_ROWS][MAX_COLS] << endl;
Ascending Column Values
By definition, a column has ascending values if column[n] < column[n + 1].
Implementing this in a for loop:
bool is_ascending = true;
for (unsigned int col = 0;
(col < MAX_COLS-1) && is_ascending;
++col)
{
if (matrix[row][col] >= matrix[row][col + 1])
{
is_ascending = false;
}
}
You may want to adjust the rule and let equal items count as ascending values.
The corners of an two dimensional array can be obtained as follows:
let an array a be = {1,2,3,4,5,6,7,8,9} Then the corner elements of the example array would be 1,3,7 and 9.
So, our Program would be like:-
#include<iostream>
using namespace std;
int main()
{
int a[][3]={1,2,3,4,5,6,7,8,9};
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
if(i%2==0||j%2==0)
cout << a[i][j];
else
cout << " ";
}
cout << endl;
}
return 0;
}
This program seems to work fine until it goes to display the data after the sort function, it will display by destination but it bypasses displaying by airline. Then it gives the access violation window and I'm not 100% sure on what it even means. If anybody could explain and help that would be much appreciated.
#include <iostream>
#include <string>
using namespace std;
double flight_number[3], number_passengers[3], max_records, max_pass, min_pass;
string airline_name[3], destination[3], duplicate[3];
char reply, swapping;
int row, index[3];
void setup();
void load_arrays();
void display_airline();
void display_destination();
void sort();
int main()
{
setup();
load_arrays();
display_airline();
display_destination();
system("pause");
return 0;
}
void setup()
{
max_records = 3;
max_pass = 275;
min_pass = 50;
}
void load_arrays()
{
for (row = 0; row < max_records; row++)
{
cout << "Charter Number " << row << endl << endl;
cout << "Please enter..." << endl;
cout << "Airline Name----> ";
cin >> airline_name[row];
cout << endl;
cout << "Flight Number----> ";
cin >> flight_number[row];
cout << endl;
cout << "Destination----> ";
cin >> destination[row];
cout << endl;
cout << "Passenger Load----> ";
cin >> number_passengers[row];
cout << endl;
while (number_passengers[row] < min_pass || number_passengers[row] > max_pass)
{
cout << "You have entered an invalid amount of passengers." << endl;
cout << "There must be between 50 and 275 passengers. > ";
cin >> number_passengers[row];
cout << endl;
}
}
}
void display_airline()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = airline_name[row];
}
sort();
cout << "Airline" << " Flight" << " Destination" << " Passenger" << endl;
cout << "Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << airline_name[index[row]] << " " << flight_number[index[row]] << " " << destination[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void display_destination()
{
system("cls");
for (row = 0; row < max_records; row++)
{
duplicate[row] = destination[row];
}
sort();
cout << "Destination" << " Flight" << " Flight" << " Passenger" << endl;
cout << " Name" << " Number" << " Load" << endl;
for (row = 0; row < max_records; row++)
{
cout << destination[index[row]] << " " << airline_name[index[row]] << " " << flight_number[index[row]] << " " << number_passengers[index[row]];
cout << endl;
}
}
void sort()
{
for (row = 0; row < max_records; row++)
{
index[row] = row;
}
swapping = 'Y';
while (swapping == 'Y')
{
swapping = 'N';
for (row = 0; row < max_records; row++)
{
if (duplicate[row] > duplicate[row + 1])
{
swap (duplicate[row], duplicate[row + 1]);
swap (index[row], index[row + 1]);
swapping = 'Y';
}
}
}
}
also this is the full violation error: Unhandled exception at 0x009984F6 in Program 6.exe: 0xC0000005: Access violation reading location 0x03947188.
for (row = 0; row < max_records; row++)
if (duplicate[row] > duplicate[row + 1]).
What's duplicate[row+1] when row==3? You want the sort to go to max_records-1 since it works on pairs of elements rather than individual elements.