Prompt user to enter two 3x3 int arrays and check equivalence - c++

Here is what I have come up with so far... I am so close with literally 1 error left for it to compile.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int SIZE = 3;
bool equals(const int m1[][SIZE], const int m2[][SIZE])
{
bool identical = true;
for (int i=0; i < SIZE && identical; i++ )
{
if (m1[i] != m2[i]){
identical = false;
}
}
return identical = true;
}
void printArray(const int m1[], int size)
{
for (int i = 0; i < size; i++)
cout << m1[i] << " ";
}
void printArray2(const int m2[], int size) //not sure if I need this 2nd
{ //void
for (int i = 0; i < size; i++)
cout << m2[i] << " ";
}
int main()
{
string input1, input2;
int const SIZE = 3;
double inputnumber;
int m2[SIZE];
int m1[SIZE];
cout << "Please enter the first array: " << endl;
getline(cin, input1);
stringstream ss (input1);
ss >> inputnumber;
cout << "Please enter the second array: " << endl;
getline(cin, input2);
stringstream si (input2);
si>>inputnumber;
for (int i=0; i< inputnumber ; ++i) {
ss >> m1[i];}
if (equals(m1, SIZE)){
cout << "The two arrays are identical ! ";
}
else{
cout << "The two arrays are NOT identical !";
}
cout << endl;
return 0;
}
Or at least I think that I am close... any help is much appreciated.
My current error is coming up on the IF statement in the main function. I could have more mistakes as I am very very new to C++. Like I said please help me out if you can.

If you just want code here it is , but if you want to practice I recommend you to implement error checking(incorrect format) in this code and to change the loops in my code into functions.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
string inp;
cout<<"Please enter an array of numbers in the following form\n"
"(where N's are any numbers) :\n"
"[N N N\nN N N\nN N N"<<endl;
double m1[3][3],m2[3][3];
for(short i=0;i<3;i++){
cout<<"> ";
if(i==0) cout<<"[ ";
getline(cin,inp);
stringstream inps(inp);
for(short j=0;j<3;j++){
inps>>m1[i][j];
}
}// Get the first array
cout<<"Now enter the second array :\n";
for(short i=0;i<3;i++){
cout<<"> ";
if(i==0) cout<<"[ ";
getline(cin,inp);
stringstream inps(inp);
for(short j=0;j<3;j++){
inps>>m2[i][j];
}
}// Get the second one
bool not_equal=false;
for(short i=0; i<3 && !not_equal ;i++)
for(short j=0; j<3 && !not_equal ;j++)
if(m1[i][j]!=m2[i][j])
not_equal=true;// Test equality
if(not_equal)
cout<<"The two arrays you entered are not equal !!"<<endl;
else
cout<<"The two arrays you entered are EQUAL ."<<endl;
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int size = 3;
int m2[size][size];
int m1[size][size];
cout << "Please enter the first array: " << endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin >> m1[i][j];
}
}
cout << "Please enter the second array: " << endl;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
cin >> m2[i][j];
}
}
int flag=1;
for(int i=0;i<size;i++)
{
for(int j=0;j<size;j++)
{
if(m2[i][j]!=m1[i][j])
{
flag=0;
}
}
}
if(flag==1)
{
cout <<"SAME"<<endl;
}
else if(flag==0)
{
cout <<"NOT SAME"<<endl;
}
return 0;
}

Related

How to end program if user enters -1 for the number in a Dynamic Array?

I am new to coding and trying to create a dynamic array that asks the user for the size of it and asks for input. It then should print. The part that I am having trouble with is that these procedures should repeat as needed or until they enter -1 for the number. I am having trouble with ending the program when they enter -1.
#include <iostream>
using namespace std;
int main()
{
int count;
cout << "How many values do you want to store in your array?";
cin >> count;
int *DynamicArray;
DynamicArray = new int[count];
for (int k = 0; k < count; k++)
{
cout << "Please input Values: ";
cin >> DynamicArray[k];
if (k == '-1') //This is the part i'm having trouble
{
cout << "The program has ended" << endl;
}
else
{
cout << endl;
}
}
for (int i = 0; i < count; i++)
{
cout << DynamicArray[i] << endl;
}
delete[] DynamicArray;
return 0;
system("pause");
}
//When I enter -1 as an input value, it continues to print it in the output. //I need it to end the program.
#include <iostream>
using namespace std;
int main()
{
int count;
cout << "How many values do you want to store in your array?";
cin >> count;
int *DynamicArray;
DynamicArray = new int[count];
for (int k = 0; k < count; k++)
{
cout << "Please input Values: ";
cin >> DynamicArray[k];
if (k == '-1') //This is the part i'm having trouble
{
if(DynamicArray[k]==-1){
delete[] DynamicArray;
cout << "The program has ended" << endl;
exit(0);
}
else
{
cout << endl;
}
}
for (int i = 0; i < count; i++)
{
cout << DynamicArray[i] << endl;
}
delete[] DynamicArray;
return 0;
system("pause");}

after inputting the values how can i display the values under one bracket e.g "{6,7,8,9}

#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
}
return 0;
}
You can use std::cout like :
#include <iostream>
using namespace std;
int main(){
int n;
cout << "No. of values : ";
cin >> n;
int array[n];
for (int i=0; i<n; i++)
{
cin >> array[i];
if(i ==0)
std::cout<<"{" <<array[i];
else if(i == n-1)
std::cout<<","<<array[i]<<"}";
else
std::cout<<","<<array[i];
}
return 0;
}
#mystic's answer uses arrays, which works fine. You can also use vector. There are more advanced methods of iterating over a vector, but I have not included that here to keep it simple.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> intVector{};
int n;
int input;
cout << "No. of values : ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> input;
intVector.push_back(input);
}
// Print out the array
cout << "{";
for(int i = 0; i < intVector.size(); i++) {
cout << intVector[i];
// print out the comma, except for the last number
if(i < intVector.size() - 1) {
cout << ", ";
}
}
cout << "}" << endl;
return 0;
}
If you want to use an iterator for printing the array, you can replace the print loop with this code:
// Print out the array
cout << "{";
for(auto i=intVector.begin(); i!=intVector.end(); ++i) {
if (i != intVector.begin()) {
cout << ", ";
}
cout << *i;
}
cout << "}" << endl;

can not swap array elements c++

I am new to C++. I am trying to solve a problem in the textbook: swap the first and last element in an array. But when I run the code I wrote, nothing happened and even the sentence "Please enter the numbers in the array: " does not show up. Anyone could give some help? Thanks.
#include <iostream>
using namespace std;
int swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
cout << "Please enter the numbers in the array: " << endl;
int input;
cin >> input;
for(int i=0; i<SIZE; i++)
{
test[i] = input;
}
swap(test, SIZE);
cout << test[SIZE] << endl;
return 0;
}
There were a few mistakes:
You should get the input inside the loop and then assign it to the test array.
When printing the swapped value, access the test array with SIZE-1 instead of SIZE, because array indexes run from 0 to SIZE-1, inclusive.
You declared swap() as returning int, but provided no return statement (this suggests that you haven't enabled enough warnings from your compiler).
#include <iostream>
using namespace std;
void swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
int input;
cout << "Please enter the numbers in the array: " << endl;
for(int i=0; i<SIZE; i++)
{
cin >> input;
test[i] = input;
}
swap(test, SIZE);
cout << test[SIZE-1] << endl;
return 0;
}
#include <iostream>
using namespace std;
//Here return type should be void as you are not returning value.
void swap(int values[], int size)
{
int temp = values[0];
values[0] = values[size-1];
values[size-1] = temp;
}
int main()
{
const int SIZE = 5;
int test[SIZE];
cout << "Please enter the numbers in the array: " << endl;
//USE LOOP TO TAKE INPUT ONE BY ONE IN AN ARRAY
for(int i = 0; i < SIZE; i++)
cin >> test[i];
swap(test, SIZE);
//USE LOOP TO DISPLAY ELEMENT ONE BY ONE
for(int i = 0; i < SIZE; i++)
cout << test[i] << endl;
return 0;
}

Inserting an element into multi dimension vector errors

I'm writing a program where I'm creating a lab with multidimensional vectors. So far I have a constructor that asks the user to input number and sizes of a lab. In the program I'm creating a function called addLab that add a new lab at the specified position with number of computers inputted by user.
While testing the program I make the user input the number of sizes of the lab to be 3 then I tried adding lab 5 to the vector but I get the following output which doesn't compile correctly:
labs: Computer Stations:
1: 1:empty 2:empty 3:empty
2: 1:empty 2:empty 3:empty
3: 1:empty 2:empty 3:empty
Where do you want the lab inserted at?
Here is my header file
#ifndef __grade_weight_calculator__ComputerLabs__
#define __grade_weight_calculator__ComputerLabs__
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs{
public:
ComputerLabs();
void show_labs();
void add_lab();
private:
vector<vector<string>> labs;
int numoflab;
int numofcomp;
int lab_numba;
int comp_numba;
};
#endif
Here is my implementation cpp file
#include "ComputerLabs.h"
ComputerLabs::ComputerLabs()
{
cout<<"Input the number of labs"<<endl;
cin>>numoflab;
cout<<"Input the size of the labs"<<endl;
cin>>numofcomp;
for (int i=0;i<numoflab; i++){
vector<string> row;
for (int j=0;j<numofcomp; j++)
row.push_back("empty");
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout<<"labs: "<<"Computer Stations:"<<endl;
int a=0;
int j;
for (int i=0;i<numoflab; i++){
cout<<a+1<<": ";
j=0;
while(j<numofcomp){
cout<<" ";
cout<<j+1<<":"<<labs[i][j];
++j;
}
a++;
cout<<endl;
}
}
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
numoflab=5;//this makes it add lab 5
vector<string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+4,row);
}
And here is my main.cpp file:
#include "ComputerLabs.h"
int main()
{
ComputerLabs mycomp;
mycomp.show_labs();
mycomp.add_lab();
mycomp.show_labs();
}
I think another error that might be causing this relates to show_labs() function in terms of the algorithm I tried using but im not entirely too sure. Can anyone help me figure out my issues and the solution for fixing it?
The main problem in your code is in the add_lab() function.
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
// PROBLEM LINE
// Not sure why you needed to do that.
// When the initial value of numoflab is 3,
// numoflabs can be changed to 4 but not 5. If you set it
// to 5 here, you run into problem in show_lab() since
// there aren't 5 rows in your 2D vector.
numoflab=5;//this makes it add lab 5
vector<string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+4,row);
}
Change that function to:
void ComputerLabs::add_lab()
{
cout<<"Where do you want the lab inserted at?"<<endl;
int pos;
cin >> pos;
// Make sure that the input position is valid.
if ( pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
// Increment numoflab by 1
++numoflab;
std::vector<std::string> tempRow;
for(int i=0;i<numofcomp;i++)
{
tempRow.push_back("empty");
}
labs.insert(labs.begin()+pos, tempRow);
}
General Improvements
You don't need to store numoflab in your class as a member variable. It can be derived from labs.
I see that you have already updated your code so that row is not a member variable of the class any more.
As a matter of good design, it's better to provide the class with its data rather than expecting the class to get the data from stdin or cin.
Here's an updated version of your program.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs {
public:
ComputerLabs(int numoflab, int numofcomp);
void show_labs();
void add_lab(int pos);
private:
vector<vector<string>> labs;
int numofcomp;
int lab_numba;
int comp_numba;
};
ComputerLabs::ComputerLabs(int numoflab, int numofcomp) : numofcomp(numofcomp)
{
for (int i = 0; i < numoflab; i++){
std::vector<std::string> row;
for (int j = 0; j < numofcomp; j++)
{
row.push_back("empty");
}
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout << "labs: " << "Computer Stations:" << endl;
int numoflab = labs.size();
for (int i = 0; i<numoflab; i++){
cout << i+1 << ": ";
for (int j = 0; j < numofcomp; j++)
{
cout<<" ";
cout << j+1 << ":" << labs[i][j];
}
cout<<endl;
}
}
void ComputerLabs::add_lab(int pos)
{
int numoflab = labs.size();
if ( pos < 0 || pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
std::vector<std::string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+pos,row);
}
int main()
{
int numoflab;
int numofcomp;
cout << "Input the number of labs" << endl;
cin >> numoflab;
cout << "Input the size of the labs" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
ComputerLabs mycomp(numoflab, numofcomp);
mycomp.show_labs();
cout << "Where do you want the lab inserted at?" << endl;
int pos;
cin >> pos;
if (!cin )
{
// Deal with error.
}
mycomp.add_lab(pos);
mycomp.show_labs();
}
Update
If you would like sizes of the newly added labs to be different from the sizes of existing labs, you can do the following:
Remove numofcomp as a member variable.
Add another argument to add_lab, numofcomp.
Before calling add_lab, ask the user for the number of components of the lab.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class ComputerLabs {
public:
ComputerLabs(int numoflab, int numofcomp);
void show_labs();
void add_lab(int pos, int numofcomp);
private:
vector<vector<string>> labs;
};
ComputerLabs::ComputerLabs(int numoflab, int numofcomp)
{
for (int i = 0; i < numoflab; i++){
std::vector<std::string> row;
for (int j = 0; j < numofcomp; j++)
{
row.push_back("empty");
}
labs.push_back(row);
}
}
void ComputerLabs::show_labs()
{
cout << "labs: " << "Computer Stations:" << endl;
int numoflab = labs.size();
for (int i = 0; i<numoflab; i++){
cout << i+1 << ": ";
int numofcomp = labs[i].size();
for (int j = 0; j < numofcomp; j++)
{
cout<<" ";
cout << j+1 << ":" << labs[i][j];
}
cout<<endl;
}
}
void ComputerLabs::add_lab(int pos,
int numofcomp)
{
int numoflab = labs.size();
if ( pos < 0 || pos > numoflab )
{
cout << "Invalid position." << endl;
return;
}
std::vector<std::string> row;
for(int i=0;i<numofcomp;i++)
{
row.push_back("empty");
}
labs.insert(labs.begin()+pos,row);
}
int main()
{
int numoflab;
int numofcomp;
cout << "Input the number of labs" << endl;
cin >> numoflab;
cout << "Input the size of the labs" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
ComputerLabs mycomp(numoflab, numofcomp);
mycomp.show_labs();
cout << "Where do you want the lab inserted at?" << endl;
int pos;
cin >> pos;
cout << "Input the size of the lab" << endl;
cin >> numofcomp;
if (!cin )
{
// Deal with error.
}
mycomp.add_lab(pos, numofcomp);
mycomp.show_labs();
}
numoflab = 5; // number of labs increased by 2 (from 3)
for(int i = 0; i < numofcomp; i++)
{
row.push_back("empty");
}
labs.insert(labs.begin() + 3, row); // you added only one lab (or row), (the program expects one more to 5)
You've got to insert one more row, do labs.insert(labs.begin() + 4, row) again for example. (labs.begin() + 3 or labs.begin() + 4 is also perfectly valid.)
You also have a very silly bug in your code, which is using vector<string> row as a member. Now, each time you push_back, row grows in size, but it's never cleared (except at the end of the program). If you printed the whole rows instead of just 3, you'd get this:
labs: Computer Stations:
1: 1:empty 2:empty 3:empty
2: 1:empty 2:empty 3:empty 4:empty 5:empty 6:empty
3: 1:empty 2:empty 3:empty 4:empty 5:empty 6:empty 7:empty 8:empty 9:empty
Very bad. You don't need row to be a member, you can make it local (create and destroy it in every function in which you insert the rows). In the ComputerLabs constructor, it can even be inside the scope of the outer loop:
for (int i = 0; i < numoflab; i++) {
vector<string> row; // constructed every iteration
for (int j = 0;j < numofcomp; j++)
row.push_back("empty");
labs.push_back(row); // row is copied
// row is destroyed
}

Simple Array Coordinate Display Matching

I am attempting to create an array which takes user input and breaks at 99 99 but the multidimensional array does not stop taking inputted values. Another issue is getting the values to display on the grid down below in the visual representation. If more clarification is needed, please let me know.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int organisms[22][22];
int rows,
columns;
int input[22][22];
int *output = new int[];
//filling array
for(int i=0;i<22;i++)
{
for(int j=0;j<22;j++)
{
organisms[i][j]=0;
}
}
//column output
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
cout<<"\t"<<columns;
}
cout<<"\n\n\n";
//Row and data output
for(int i=0;i<20;i++)
{
cout<<"Row "<<i+1<<"\t";
for(int j=0;j<20;j++)
{
cout<<organisms[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n\n\n";
cout<<"Enter each cell in first colony use row space column Enter format, 3 4, for example.\nEnter 99 99 to end entries.\n";
//input of values
cin.ignore(4);
int m=0;
do
{
cin>>input[m][m];
m++;
if(input[m][m]!=99,99)
break;
}while(m<20);
/*
while(input[m][m]!=99,99)
{
cin>>input[m][m];
m++;
}
/*
for(int m=0;m<INT_MAX;m++)
{
cin>>input[m][m];
if(input[m][m]==99)
break;
}
*/
//input array indexing
//output array
cout<<"\n\n\n";
cout<<"\t\t\t\t\t\t\t\t\t\t\t\t\tColumn\n\n";
for(columns=1;columns<21;columns++)
{
cout<<"\t"<<columns;
}
cout<<"\n\n\n";
for(int i=0;i<20;i++)
{
cout<<"Row "<<i+1<<"\t";
cout<<endl;
for(int n=0;n<20;n++)
{
cout<<input[i][n]<<"\t";
}
cout<<endl;
}
cin.get();
cin.get();
return 0;
}
if(input[m][m]!=99,99)
your input is an array of int, 99,99 is not an int. use valid int value, 5555 for example, or simple 99
E.g)
#include <iostream>
#include <iomanip>
#include <utility>
#include <vector>
#include <iterator>
using namespace std;
const size_t size=20;
char table[size][size];
void table_clear(void){
for(int v = 0; v<size; ++v)
for(int h = 0; h<size; ++h)
table[v][h]=' ';
}
void table_put(pair<int, int> p){
int v = p.first;
int h = p.second;
table[v-1][h-1] = '*';//to zero origin
}
void table_disp(){
cout << " ";
for(int i=0; i<size;++i){
cout << setw(3) << i + 1;
}
cout << "\n" << endl;
for(int i=0;i<size;++i){
cout << setw(3) << left << i + 1;
for(int j=0;j<size;++j){
cout << setw(3) << right << table[i][j];
}
cout << endl;
}
}
int main(){
vector<pair<int,int> > v;
pair<int, int> end(99,99);
pair<int, int> rc;
cout << "Enter each cell in first colony use row space column Enter format, 3 4, for example.\n";
cout << "Enter 99 99 to end entries." << endl;
while(true){
int row, column;
cin >> row;
cin >> column;
rc = make_pair(row, column);
if(rc == end)
break;
v.push_back(rc);
}
table_clear();
for(vector<pair<int,int> >::const_iterator iter = v.begin();iter != v.end(); ++iter ) {
table_put(*iter);
}
table_disp();
}