Am i filling this array wrong or outputting it wrong? - c++

I am working on a program that fills an array with data from a text file. When I output the array its contents are not in the order I thought I read them in. I'm thinking the problem is either in one of the for loops that inputs data into the array or outputs the array to the iostream. Can anyone spot my mistake?
The data:
(I changed the first number in each row to 2-31 to differentiate it from the 0's and 1's)
The output:
The code:
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
int FC_Row, FC_Col, EconRow, EconCol, seat, a, b;
inFile.open("Airplane.txt");
inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;
int airplane[100][6];
int CurRow = 0;
int CurCol = 0;
while ( (inFile >> seat) && (CurRow < FC_Row))
{
airplane[CurRow][CurCol] = seat;
++CurCol;
if (CurCol == FC_Col)
{
++CurRow;
CurCol = 0;
}
}
while ( (inFile >> seat) && (CurRow < EconRow))
{
airplane[CurRow][CurCol] = seat;
++CurCol;
if (CurCol == EconCol)
{
++CurRow;
CurCol = 0;
}
}
cout << setw(11)<< "A" << setw(6) << "B"
<< setw(6) << "C" << setw(6) << "D"
<< setw(6) << "E" << setw(6) << "F" << endl;
cout << " " << endl;
cout << setw(21) << "First Class" << endl;
for (a = 0; a < FC_Row; a++)
{
cout << "Row " << setw(2) << a + 1;
for (b = 0; b < FC_Col; b++)
cout << setw(5) << airplane[a][b] << " ";
cout << endl;
}
cout << setw(23) << "Economy Class" << endl;
for (a = 6; a < EconRow; a++)
{
cout <<"Row " << setw(2)<< a + 1;
for (b = 0; b < EconCol; b++)
cout << setw(5) << airplane[a][b] << " ";
cout << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}

You're filling it wrong.
for (a = 0; a < 100; a++)
for (b = 0; b < 6; b++)
The above loop doesn't match up very well with the first lines of your file, where you don't have 6 elements per row.
In the first inner loop, you will read 2, 1, 1, 1, 3, 0 into airplane[0].
EDIT: The fix.
for (a = 0; a < FC_Row; a++)
for (b = 0; b < FC_Col; b++)
inFile >> airplane[a][b] ;
for (a = 0; a < EconRow; a++)
for (b = 0; b < EconCol; b++)
inFile >> airplane[a+FC_Row][b] ;

Your code that fills the array:
for (a = 0; a < 100; a++)
for (b = 0; b < 6; b++)
inFile >> airplane[a][b] ;
assumes that there are 6 columns in every row, there aren't, there are only 4 rows in the first 6 rows.

so you're filling in a 100x6 array, but first few rows of data only have 4 columns of data.
A better way is something like this:
for (a = 0; a < 100; a++)
for (b = 0; b < 6; b++)
{
char c;
inFile>>c;
if (c is new line){
break;
}
//fill in the 2d array
}

The correct approach here is to read in a line at a time with std::getline. Then parse each line, similar to the way you are, albeit you might want to use vectors rather than 2-dimensional arrays.
If you had a vector of vectors, you would find that the inner vectors do not need to all have the same size, and indeed they should not in your case.
As it is, what I do not get is that you are reading in the values for EconRow and EconCol yet hard-coding your array size.
With vector you would be able to flexibly set this to the value you had read in.

Related

Creating a header column via "for" loop in a multiplication table produces gibberish instead of desired result

I need to create a multiplication table of a number between 1 and 20, I've did just that using 2 nested for loops. What I'm having problems with is the formatting of the table - the output in the console needs to look exactly like your average multiplication table in a schoolbook, horizontal and vertical row of numbers and when you find the cross between the horizontal and the vertical numbers you find your multiplication. I've done the horizontal row using a "first pass" check as you can see in the code below:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
int firstpass = 1;
cin >> br;
if (br > 1 && br < 21)
{
if (firstpass = 1)
{
for (int h = 0; h <= br; h++)
cout << setw(5) << h << " ";
cout << endl;
firstpass = 0;
}
for (int a = 1; a <= br; a++)
{
for (int b = 1; b <= br; b++)
cout << setw(5) << a * b << " ";
cout << endl;
}
}
return 0;
}
but the problem with the code above is that it doesn't produce a vertical column (in front of each output in the nested loop should be the number of a), so I added a "second pass" check. The goal was to check if it's a start of a new row via "secondpass", if it is, output the current value of "a" then lie dormant until the loop ends and secondpass is reset, but instead of just adding the value of "a" at the start of the row and then outputting the rest of the row like the first example does, it's showing pure gibberish that I can't untangle.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
int firstpass = 1;
int secondpass = 1;
cin >> br;
if (br > 1 && br < 21)
{
if (firstpass = 1)
{
for (int h = 0; h <= br; h++)
cout << setw(5) << h;
cout << endl;
firstpass = 0;
}
for (int a = 1; a <= br; a++)
{
for (int b = 1; b <= br; b++)
{
if (secondpass = 1)
{
cout << setw(5) << a;
secondpass = 0;
}
cout << setw(5) << a << a * b;
}
secondpass = 1;
cout << endl;
}
}
return 0;
}
What am I doing wrong? I'm sure this fever I'm under isn't exactly helping but I'm feeling really dumb right now. Thanks in advance!
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int br;
cin >> br;
if (br > 1 && br < 21)
{
for (int a = 0; a <= br; a++)
{
for (int b = 0; b <= br; b++)
{
if (b == 0)
cout << setw(5) << a;
else if (a == 0)
cout << setw(5) << b;
else
cout << setw(5) << a * b;
}
cout << endl;
}
}
return 0;
}
Here's the answer for someone down the line, finally cracked it. Still no idea why the nested for loops were acting spooky when trying to add another character the original way I did but I guess that's a question for another time.

Difference of 2 sets retained in arrays - C++

Consider two sets retained in two arrays. Find the union, intersection and difference (relative complement) of the two sets.
I managed to solve the union and the intersection, but the difference is giving me a hard time. Any hints? And if possible, keep it as simple as possible, without functions or more complex aspects, because I'm a beginner and I still have a lot to learn.
Thank you in advance!
#include <iostream>
using namespace std;
int main()
{
int v1[100], v2[100], u[200], intersection[100], d[100];
unsigned int v1_length, v2_length, i, j, OK = 0, union_length;
cout << "Enter the number of elements of the first array:" << " ";
cin >> v1_length;
cout << "Enter the elements of the first array:" << '\n';
for (i = 0; i < v1_length; i++)
cin >> v1[i];
cout << "Enter the number of elements of the second array:" << " ";
cin >> v2_length;
cout << "Enter the elements of the second array:" << '\n';
for (i = 0; i < v2_length; i++)
cin >> v2[i];
//Union
union_length = v1_length;
for (i = 0; i < v1_length; i++)
u[i] = v1[i];
for (i = 0; i < v2_length; i++)
{
int ok = 0;
for (j = 0; !ok && j < v1_length; j++)
if (v1[j] == v2[i])
ok = 1;
if (!ok)
{
u[union_length] = v2[i];
union_length++;
}
}
cout << "The union of the two sets contained in the arrays is: ";
for (i = 0; i < union_length; i++)
cout << u[i] << " ";
cout << '\n';
//Intersection
unsigned int k = 0;
cout << "The intersection of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++)
if (v1[i] == v2[j])
{
intersection[k] = v1[i];
k++;
}
for (i = 0; i < k; i++)
cout << intersection[i] << " ";
cout << '\n';
//Difference
unsigned int l = 0, OK2 = 0;
cout << "The difference of the two sets contained in the arrays is: ";
for (i = 0; i < v1_length; i++)
{
for (j = 0; j < v2_length; j++)
{
if (v1[i] == v2[j])
OK2 = 1;
if (!OK2)
{
d[l] = v1[i];
l++;
}
}
}
for (i = 0; i < l; i++)
cout << d[i] << " ";
cout << '\n';
return 0;
}
It seems that the intersection is the best place to start. You want the items that only in appear in one of the two arrays, right?
So, for the inner loop, you need to compare all the elements. Then, if no match was found, you have the a unique element.
You need to add the curly braces {} to the for loop. I know that curly braces are distracting at times, but over time, you will probably find it safer to almost always include them to avoid confusion.
for (i = 0; i < v1_length; i++)
for (j = 0; j < v2_length; j++) {
if (v1[i] == v2[j]){
break; // this item is not unique
} else if(j == v2_length - 1){
d[l] = v1[i]; // This is the unique one, add it to the answer array
l++;
}
}
for (i = 0; i < l; i++)
cout << intersection[l] << " ";
cout << '\n';
You're on the right track!
You're doing a few things wrong. Here are some fixes you can try:
Only set OK2 to 0 once per inner-loop
Reset OK2 to 0 at the end of the inner-loop
Only do the insertion into d after the inner-loop has completed
As an optimization, consider breaking after you set OK2 to 1, as you know at that point it can never be set to 0 for the current value pointed to by the outer-loop.

Program that receives 2 arrays and checks how many times 1 is included in the other

I need to write a program that receives 2 arrays and checks how many times 1 is included in the other...
But I cant find what is wrong with my program! tx!!
#include <iostream>
using namespace std;
int main()
{
int vector1[500];
int vector2[100];
int a = 0, b = 0, count = 0, k = 0;
cout << "enter size of first array:" << endl;
cin >> a;
cout << " enter first array values:" << endl;
for (int i = 0; i < a; i++)
cin >> vector1[i];
cout << "enter size of second array:" << endl;
cin >> b;
cout << "enter secound array values:" << endl;
for (int i = 0; i < b; i++)
cin >> vector2[i];
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i + k] == vector1[j])
{
count++;
k++;
}
else
k = 0;
cout << count << endl;
system("pause");
return 0;
}
Why at all do you need k? The problem is about all inclusions of all elements right? If O(n^2) complexity is fine, then...
for (int i = 0; i < b; i++)
for (int j = 0; j < a; j++)
if (vector2[i] == vector1[j])
count++;
One obvious disadvantage of the code above is that you'll get the total sum of all occurences of elements from vector1 in vector2. The key idea remains the same in case you need to know, which elements exactly appeared in another array and how many times, you'll just have to use map or other vector.

C++ function and array homework

Create a function that prints the array to the screen as a table with the appropriate rows and columns. Use setw() to ensure the numbers have enough room. You may assume the numbers are no more than 3 digits each. Include the row and column labels.
In main, ask the user to supply a row and column and a value (no more than 3 digits), then put the value into the array at that row and column. Print the result and ask the user for another row, column and value. Repeat until the user is finished.
Once finished, compute and print the total of the values in the array.
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS= 4;
const int COLS = 3;
const int WIDTH = 4;
const char YES = 'y';
int total = 0;
void print(int arr[ROWS][COLS]);
int main()
{
int arr[ROWS][COLS];
char ans = YES;
int val;
for (int r = 0; r < ROWS; r++){
for (int c = 0; c < COLS; c++)
arr[r][c] = 0;}
while (tolower(ans) == YES){
int row = -1;
int col = -1;
cout << "Row? ";
cin >> row;
cout << "Columns? ";
cin >> col;
while (row < 0 || row >=ROWS){
cout << "Only value under " << ROWS << " is accepted, try again: ";
cin >> row;
}
while (col < 0 || col >= COLS){
cout << "Only value under " << COLS << "is accepted, try again: ";
cin >> col;
}
cout << "Value? ";
cin >> val;
arr[row][col] = val;
print(arr);
cout << "Again (y/n)? ";
cin >> ans;
}
for (int r = 0; r < ROWS; r++){
for (int c = 0; c < COLS; c++){
total += arr[r][c];
}
}
cout << "Sum of all values is " << total << endl;
// Print array with labels
// get input from user
// print array again - repeat until user wishes to quit
return 0;
}
void print (const int arr[ROWS][COLS])
{
cout << endl << endl;
for (int i = 0 ; i < COLS; i++)
cout << setw(WIDTH) << i;
cout << endl;
for (int r = 0; r < ROWS; r++)
cout << setw(WIDTH) << r << "|";
for (int r = 0; r < ROWS; r++)
for (int c = 0; c< COLS; c++)
cout << setw(WIDTH) << arr[r][c];
cout << endl << endl;
}
I dont know where I did wrong on this one but when I complie, I got the LD return 1 exit status error, Can you please help?
The definition and the declaration of your print function are different.
Change the declaration of your print() function.
void print(int arr[ROWS][COLS]);
to
void print(const int arr[ROWS][COLS]);

Row transposition cypher using extra column

Hi guys I am trying to make a program that takes some user input and maps it to a 2d array and then encrypts it by mixing up the columns. For example if the user enters "my name is fred" the program creates an array that is 3x6 filling the last column with y's and the remain empty spaces with x's so it should be something like
mynamy
eisfry
edxxxx
instead I wind up with
mynam
eisfr
edxx
#include <iostream>
#include<cctype>
#include<algorithm>
using namespace std;
main(){
string input;
cout << "Enter information to be encrypted" << endl;
getline(cin,input);
input.erase(std::remove (input.begin(), input.end(), ' '), input.end());
int columns = 6;
int rows;
if (input.size() <= 5){
rows = 1;
}
else if (input.size()% 5 > 0){
rows = input.size()/5 + 1;
}
else
rows = input.size()/5;
char message[rows][columns];
int place = 0;
for(int i = 0; i < rows; i++){
for(int j = 0; j < (columns-1); j++){
if(place <= input.size()){
message[rows][columns] = input[place];
}
else {
message[rows][columns] = 'x';
}
place++;
message[rows][5] = 'y';
cout << message[rows][columns];
}
cout << endl;
}
}
this should do it..
#include <iostream>
#include <cctype>
#include <algorithm>
using namespace std;
int main()
{
string input;
cout << "Enter information to be encrypted" << endl;
getline(cin,input);
input.erase(std::remove (input.begin(), input.end(), ' '), input.end());
int columns = 6;
int rows;
if (input.size() <= 5){
rows = 1;
}
else if (input.size()% 5 > 0){
rows = input.size()/5 + 1;
}
else
rows = input.size()/5;
char message[rows][columns];
for(int i = 0; i < rows; i++){
for(int j = 0; j < (columns-1); j++){
if ((i*5 + j) < int(input.size())){
message[i][j] = input[i*5 + j];
}
else {
message[i][j] = 'x';
}
// place++;
if (i != rows-1) message[i][5] = 'y';
else message[i][5] = 'x';
// cout << "i: " << i << " | j: " << j << " | " << message[i][j] << endl;
}
cout << endl;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
cout << message[i][j];
}
cout << " ";
}
cout << endl;
}
Your code isn't actually doing anything to transpose the matrix! It's writing the message into the matrix, but it's printing each entry out right after it's written, so it doesn't end up changing the order at all.
You'll need a separate set of loops to read data out of the matrix.