Finding determinate and Multiplication two matrixes with different length in c++ - c++

Hey guys I want to make a calculator for matrixes but I have problems.
first, when I want to multiply them it will work when columns and rows are equal in the matrixes but when it's different like n1=5 m1=2 n2=2 m2=6 it goes wrong and have some bugs
and the bigger problem is for calculating the determinate I have no idea what should I do
thanks for answering.
this the code
#include <iostream>
using namespace std;
int main() {
int n1, m1, n2, m2;
int counter = 0;
int ask;
bool matrixCheck;
int determinant = 0;
cout << "pls enter N1" << endl;
cin >> n1;
while (n1 > 100) {
cout << "N most be lower than 100";
cin >> n1;
}
cout << "pls enter M1" << endl;
cin >> m1;
while (m1 > 100) {
cout << "M most be lower than 100";
cin >> m1;
}
cout << "pls enter N2" << endl;
cin >> n2;
while (n2 > 100) {
cout << "N most be lower than 100";
cin >> n2;
}
cout << "pls enter M2" << endl;
cin >> m2;
while (m2 > 100) {
cout << "M most be lower than 100";
cin >> m2;
}
int matrixA[n1][m1];
int matrixB[n2][m2];
cout << "ENTER MATRIX A" << endl;
for (int i = 0; i <= n1 - 1; i++) {
cout << " ";
cout << counter + 1 << "," << i + 1 << "= ";
cin >> matrixA[counter][i];
cout << " ";
if (i == n1 - 1) {
cout << endl;
counter++;
i = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[counter][j] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
counter = 0;
cout << "ENTER MATRIX B" << endl;
for (int k = 0; k <= n2 - 1; k++) {
cout << " ";
cout << counter + 1 << "," << k + 1 << "= ";
cin >> matrixB[counter][k];
cout << " ";
if (k == n2 - 1) {
cout << endl;
counter++;
k = -1;
}
if (counter == m2) {
break;
}
}
counter = 0;
cout << "This is B" << endl;
for (int q = 0; 1 <= n2 - 1; q++) {
cout << matrixB[counter][q] << " ";;
if (q == n2 - 1) {
cout << endl;
counter++;
q = -1;
}
if (counter == m2) {
break;
}
}
cout << "Choose an operation" << endl
<< "1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)" << endl;
if (n1 != n2 || m1 != m2) {
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
matrixCheck = false;
} else if (n1 == n2 && m1 == m2) {
matrixCheck = true;
}
cin >> ask;
while (ask == 1 || ask == 2 || ask == 5 || ask == 6) {
if (matrixCheck) {
break;
}
cout
<< "We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different"
<< endl;
cin >> ask;
}
if (ask == 1 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] + matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 2 && matrixCheck) {
counter = 0;
for (int n = 0; n <= n1 - 1; n++) {
matrixA[counter][n] = matrixA[counter][n] - matrixB[counter][n];
if (n + 1 == n1) {
n = -1;
if (counter + 1 == m1) {
break;
}
counter++;
}
}
counter = 0;
cout << "This is A" << endl;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixA[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m1) {
break;
}
}
}
if (ask == 3) {
int matrixResult[n1][m1];
int sum = 0;
// Multiplying two matrices...
for (int i = 0; i < n1; i++) {
for (int j = 0; j < m2; j++) {
sum = 0;
for (int k = 0; k < m1; k++)
sum = sum + (matrixA[i][k] * matrixB[k][j]);
matrixResult[i][j] = sum;
}
}
cout << "\nMultiplication Result:\n";
counter = 0;
for (int j = 0; j <= n1 - 1; j++) {
cout << matrixResult[j][counter] << " ";;
if (j == n1 - 1) {
cout << endl;
counter++;
j = -1;
}
if (counter == m2) {
break;
}
}
}
}
and this the result
pls enter N1
3
pls enter M1
2
pls enter N2
2
pls enter M2
3
ENTER MATRIX A
1,1=3
1,2=3
1,3=3
2,1=3
2,2=3
2,3=3
This is A
3 3 3
3 3 3
ENTER MATRIX B
1,1=3
1,2=3
2,1=3
2,2=3
3,1=3
3,2=3
This is B
3 3
3 3
3 3
Choose an operation
1-(+) 2-(-) 3-(A * B) 4-(A / B) 5-(A --> B) 6-(B --> A) 7-(a * A) 8-(|A|) 9-(|B|)
We have to note that you can't use operaroin number 1, 2, 5 and 6 because the length of the matrixs are different
3
Multiplication Result:
18 18 12600660
18 18 12600660
18 12600660 364487398
Process finished with exit code 0

Size of resulting matrix is n1 * m2, not n1*m1.

Related

Error C++ invalid types 'int[int]' for array subscript

I have a problem with this code. I don't know why it's wrong. I have no idea how to fix it.
I started learning C++ not long time ago, so sorry if the question is stupid.
This is a university exercise to special mathematics, and we have a teacher who didn't explain anything.
P.S. Visual Studio Code, compiler MinGW
Thanks ~
enter image description here
#include <iomanip>
#include <conio.h>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <windows.h>
using namespace std;
int **matriceAdiacenta;
int matriceAdiacentaLungime = 0;
int **matriceIncidenta;
int matriceIncidentaLungime = 0,
matriceIncidentaArcs = 0;
int **ListAdiacentaiacenta;
int ListAdiacentaiacentaLungime = 0;
int meniu, tipcitireMeniu;
int **Virfulmatrice;
int virfmatrice=0;
fstream citireadinFisier;
void displayMtrAdiacenta()
{
system("cls");
if (matriceAdiacentaLungime < 1)
cout << "Va rugam sa introduceti o matrice adiacenta" << endl;
else
{
int i, j;
cout << "Matricea de adiacenta:" << endl;
for (i = 1; i <= matriceAdiacentaLungime; i++)
{
for (j = 1; j <= matriceAdiacentaLungime; j++)
cout << matriceAdiacenta[i][j] << " ";
cout << endl;
}
}
cout << endl
<< "Tastati oricare buton pentru revenirea la meniu";
getch();
}
void displayMatriceaIncidenta()
{
system("cls");
if (matriceIncidentaLungime < 1)
cout << "Va rugam sa introduceti o matrice de incidenta" << endl;
else
{
int i, j;
cout << "Matricea de incidenta:" << endl;
for (i = 1; i <= matriceIncidentaArcs; i++)
{
for (j = 1; j <= matriceIncidentaLungime; j++)
cout << matriceIncidenta[i][j] << " ";
cout << endl;
}
}
cout << endl
<< "Tastati oricare buton pentru revenirea la meniu";
getch();
}
void displayListAdiacentaiacenta()
{
system("cls");
if (ListAdiacentaiacentaLungime < 1)
cout << "Va rugam sa introduceti o lista de adiacenta" << endl;
else
{
cout << "Lista de adiacenta:" << endl;
for (i = 0; i < ListAdiacentaiacentaLungime; i++)
{
cout << i + 1 << " - ";
j = 0;
while (ListAdiacentaiacenta[i][j] != 0)
{
cout << ListAdiacentaiacenta[i][j] << " ";
j++;
}
cout << "0" << endl;
}
cout << endl << "Apasa orice pentru a merge la meniul principal";
getch();
}
}
void StergereArc(){
int i,x,y,j,m;
virfmatrice=0;
matriceIncidentaArcs=0;
return;
for (i=0;i<m;i++) {
matriceIncidentaArcs[x][y]=matriceIncidentaArcs[x+1][y+1];
virfmatrice[y][j]=virfmatrice[y+1][j+1];
}
x=x-1;
y=y-1;
i=i-1;
j=j-1;
}
void StergereVirf(){
int i,j,p,n;
matriceIncidentaArcs=0;
for (i=0;i<n;i++) {
for (j=0;j<n;j++){
matriceIncidentaArcs[j][p]=0;
matriceIncidentaArcs[i][p]=0;
matriceIncidentaArcs[p][j]=0;
}
}
}
void insertMatriceAdiacenta(int tip)
{
int i = 0,
j = 0;
if (tip == 1)
{
citireadinFisier.open("matrAd.txt", ios::in);
citireadinFisier >> matriceAdiacentaLungime;
matriceAdiacenta = new int *[matriceAdiacentaLungime + 1];
for (i = 1; i <= matriceAdiacentaLungime; i++)
{
matriceAdiacenta[i] = new int[matriceAdiacentaLungime];
for (j = 1; j <= matriceAdiacentaLungime; j++)
{
citireadinFisier >> matriceAdiacenta[i][j];
}
}
citireadinFisier.close();
displayMtrAdiacenta();
}
else
{
cout<<"Lungimea: ";
cin >> matriceAdiacentaLungime;
matriceAdiacenta = new int *[matriceAdiacentaLungime + 1];
for (i = 1; i <= matriceAdiacentaLungime; i++)
{
matriceAdiacenta[i] = new int[matriceAdiacentaLungime];
for (j = 1; j <= matriceAdiacentaLungime; j++)
{
cin >> matriceAdiacenta[i][j];
}
}
displayMtrAdiacenta();
}
}
void insertMatriceIncidenta(int tip)
{
int i = 0,
j = 0;
if (tip == 1)
{
citireadinFisier.open("matrInc.txt", ios::in);
citireadinFisier >> matriceIncidentaLungime >> matriceIncidentaArcs;
matriceIncidenta = new int *[matriceIncidentaArcs];
for (i = 1; i <= matriceIncidentaArcs; i++)
{
matriceIncidenta[i] = new int[matriceIncidentaLungime];
for (j = 1; j <= matriceIncidentaLungime; j++)
citireadinFisier >> matriceIncidenta[i][j];
}
citireadinFisier.close();
displayMatriceaIncidenta();
}
else
{
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
}
void insertListAdiacenta(int tip)
{
int i = 0,
j = 0;
if (tip == 1)
{
citireadinFisier.open("LstInc.txt", ios::in);
citireadinFisier >> ListAdiacentaiacentaLungime;
ListAdiacentaiacenta = new int *[ListAdiacentaiacentaLungime];
while (!citireadinFisier.eof())
{
ListAdiacentaiacenta[i] = new int[ListAdiacentaiacentaLungime];
j = 0;
do
{
citireadinFisier >> ListAdiacentaiacenta[i][j];
j++;
}
while (ListAdiacentaiacenta[i][j - 1] != 0);
i++;
}
citireadinFisier.close();
displayListAdiacentaiacenta();
}
else
{
cout << "Lungimea: ";
cin >> ListAdiacentaiacentaLungime;
ListAdiacentaiacenta = new int *[ListAdiacentaiacentaLungime];
for (i = 0; i < ListAdiacentaiacentaLungime; i++)
{
ListAdiacentaiacenta[i] = new int[ListAdiacentaiacentaLungime];
j = 0;
cout << i << " - ";
do
{
cin >> ListAdiacentaiacenta[i][j];
if (ListAdiacentaiacenta[i][j] <= ListAdiacentaiacentaLungime)
j++;
} while (ListAdiacentaiacenta[i][j] != 0);
}
citireadinFisier.close();
}
}
void matriceAdiacenta_ListAdiacenta()
{
system("cls");
if (matriceAdiacentaLungime == 0)
{
cout << "Va rugam sa introduceti o matrice adiacenta" << endl;
}
else
{
int i, j;
for (i = 1; i <= matriceAdiacentaLungime; i++)
{
cout << i << " - ";
for (j = 1; j <= matriceAdiacentaLungime; j++)
{
if (matriceAdiacenta[i][j] == 1)
{
cout << j << " ";
}
}
cout << "0" << endl;
}
}
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
void matriceAdiacenta_MatriceIncident()
{
system("cls");
if (matriceAdiacentaLungime == 0)
{
cout << "Va rugam sa introduceti o matrice adiacenta" << endl;
}
else
{
int arcs = 0, i, j, currArc = 1, **matr;
for (i = 1; i <= matriceAdiacentaLungime; i++)
for (j = 1; j <= matriceAdiacentaLungime; j++)
if (matriceAdiacenta[i][j] == 1)
arcs++;
matr = new int *[arcs];
for (i = 1; i <= arcs; i++)
{
matr[i] = new int[matriceAdiacentaLungime];
for (j = 1; j <= matriceAdiacentaLungime; j++)
matr[i][j] = 0;
}
for (i = 1; i <= matriceAdiacentaLungime; i++)
for (j = 1; j <= matriceAdiacentaLungime; j++)
if (matriceAdiacenta[i][j] == 1)
{
matr[currArc][i] = -1;
matr[currArc][j] = 1;
currArc++;
}
for (i = 1; i <= arcs; i++)
{
for (j = 1; j <= matriceAdiacentaLungime; j++)
cout << matr[i][j] << " ";
cout << endl;
}
}
cout << endl
<< "Apasa orice pentru a merge la meniul principal";
getch();
}
void matriceIncidenta_matriceAdiacenta()
{
system("cls");
if (matriceIncidentaLungime == 0)
{
cout << "Va rugam sa introduceti o matrice de incidenta" << endl;
}
else
{
int **matr, matr_out, matr_in, i, j;
matr = new int *[matriceIncidentaLungime];
for (i = 1; i <= matriceIncidentaLungime; i++)
{
matr[i] = new int[matriceIncidentaLungime];
for (j = 1; j <= matriceIncidentaLungime; j++)
matr[i][j] = 0;
}
for (i = 1; i <= matriceIncidentaArcs; i++)
{
for (j = 1; j <= matriceIncidentaLungime; j++)
if (matriceIncidenta[i][j] == -1)
matr_out = j;
else if (matriceIncidenta[i][j] == 1)
matr_in = j;
matr[matr_out][matr_in] = 1;
}
//afisare
for (i = 1; i <= matriceIncidentaLungime; i++)
{
for (j = 1; j <= matriceIncidentaLungime; j++)
cout << matr[i][j] << " ";
cout << endl;
}
}
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
void matriceIncidenta_ListAdiacenta()
{
system("cls");
if (matriceIncidentaLungime == 0)
{
cout << "Va rugam sa introduceti o matrice de incidenta" << endl;
}
else
{
int i, j, k;
for (j = 1; j <= matriceIncidentaLungime; j++)
{
cout << j << " - ";
for (i = 1; i <= matriceIncidentaArcs; i++)
if (matriceIncidenta[i][j] == -1)
for (k = 1; k <= matriceIncidentaLungime; k++)
if (matriceIncidenta[i][k] == 1)
{
cout << k << " ";
break;
}
cout << "0" << endl;
}
}
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
void ListAdiacenta_matriceIncidenta()
{
system("cls");
if (ListAdiacentaiacentaLungime == 0)
{
cout << "Va rugam sa introduceti o matrice adiacenta" << endl;
}
else
{
int k = 0, i, j, **matr;
for (i = 0; i < ListAdiacentaiacentaLungime; i++)
{
j = 0;
while (ListAdiacentaiacenta[i][j] != 0)
{
k++;
j++;
};
}
matr = new int *[k];
for (i = 1; i <= k; i++)
{
matr[i] = new int[ListAdiacentaiacentaLungime];
for (j = 1; j <= ListAdiacentaiacentaLungime; j++)
{
matr[i][j] = 0;
}
}
k = 1;
for (i = 0; i < ListAdiacentaiacentaLungime; i++)
{
j = 0;
while (ListAdiacentaiacenta[i][j] != 0)
{
matr[k][i + 1] = -1;
matr[k][ListAdiacentaiacenta[i][j]] = 1;
k++;
j++;
};
}
for (i = 1; i <= k - 1; i++)
{
for (j = 1; j <= ListAdiacentaiacentaLungime; j++)
cout << matr[i][j] << " ";
cout << endl;
}
}
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
void ListAdiacenta_matriceAdiacenta()
{
system("cls");
if (ListAdiacentaiacentaLungime == 0)
{
cout << "Va rugam sa introduceti o lista de adiacenta" << endl;
}
else
{
int **matr, i, j;
matr = new int *[ListAdiacentaiacentaLungime];
for (i = 1; i <= ListAdiacentaiacentaLungime; i++)
{
matr[i] = new int[ListAdiacentaiacentaLungime];
for (j = 1; j <= ListAdiacentaiacentaLungime; j++)
matr[i][j] = 0;
}
for (i = 0; i < ListAdiacentaiacentaLungime; i++)
{
j = 0;
while (ListAdiacentaiacenta[i][j] != 0)
{
matr[i + 1][ListAdiacentaiacenta[i][j]] = 1;
j++;
};
}
for (i = 1; i <= ListAdiacentaiacentaLungime; i++)
{
for (j = 1; j <= ListAdiacentaiacentaLungime; j++)
cout << matr[i][j] << " ";
cout << endl;
}
}
cout << endl<< "Apasa orice pentru a merge la meniul principal";
getch();
}
int main()
{
bool stop = false;
while (!stop)
{
cout << "1 - Introduceti un graf" << endl;
cout << "2 - Afisarea grafului" << endl;
cout << "3 - Afisati alt tip prin schimbarea celui existent" << endl;
cout << "4 - Stergere" << endl;
cout << "5 - Iesire" << endl;
cin >> meniu;
if (meniu == 1)
{
system("cls");
cout << "1 - Matrice de adiacenta" << endl;
cout << "2 - Matrice de incidenta" << endl;
cout << "3 - Lista de adiacenta" << endl;
cin >> meniu;
system("cls");
cout << "1 - Din fisier" << endl;
cout << "2 - De la tastatura" << endl;
cin >> tipcitireMeniu;
if (meniu == 1)
insertMatriceAdiacenta(tipcitireMeniu);
else if (meniu == 2)
insertMatriceIncidenta(tipcitireMeniu);
else if (meniu == 3)
insertListAdiacenta(tipcitireMeniu);
system("cls");
}
else if (meniu == 2)
{
system("cls");
cout << "1 - Matrice de adiacenta" << endl;
cout << "2 - Matrice de incidenta" << endl;
cout << "3 - Lista de adiacenta" << endl;
cout << "next ";
cin >> meniu;
if (meniu == 1)
displayMtrAdiacenta();
if (meniu == 2)
displayMatriceaIncidenta();
if (meniu == 3)
displayListAdiacentaiacenta();
system("cls");
}
else if (meniu == 3)
{
system("cls");
cout << "1 - Matrice Adiacenta -> Lista Adiacenta" << endl;
cout << "2 - Matrice Adiacenta -> Matrice Incidenta" << endl;
cout << "3 - Matrice Incidenta -> Matrice Adiacenta" << endl;
cout << "4 - Matrice Incidenta -> Lista Adiacenta" << endl;
cout << "5 - Lista Adiacenta -> Matrice Incidenta" << endl;
cout << "6 - Lista Adiacenta -> Matrice Adiacenta" << endl;
cin >> meniu;
switch (meniu)
{
case 1:
matriceAdiacenta_ListAdiacenta();
break;
case 2:
matriceAdiacenta_MatriceIncident();
break;
case 3:
matriceIncidenta_matriceAdiacenta();
break;
case 4:
matriceIncidenta_ListAdiacenta();
break;
case 5:
ListAdiacenta_matriceIncidenta();
break;
case 6:
ListAdiacenta_matriceAdiacenta();
break;
default:
break;
}
system("cls");
}
else if (meniu == 4)
{
cout << "1 - Stergerea arcului" << endl;
cout << "2 - Stergerea virfului" << endl;
cin >> meniu;
switch (meniu)
{
case 1:
StergereArc();
break;
case 2:
StergereVirf();
break;
default:
break;
}
system("cls");}
else
{
stop = true;
}
}
return 0;
}
Your code has multiple problems, but the one you ask about is this:
Among the declarations at the top, you have:
int matriceIncidentaLungime = 0,
matriceIncidentaArcs = 0;
This declares two different ints, and initializes both to zero. Using comma to declare multiple variables on one line is legal, but something you should stop doing right away - it is a real code smell. Instead, you should write:
int matriceIncidentaLungime = 0;
int matriceIncidentaArcs = 0;
But the real bug, is that you are using matriceIncidentaArcs as it was declared as a 2-dimensional arrays, and not as a plain int:
matriceIncidentaArcs[x][y]=matriceIncidentaArcs[x+1][y+1];
For this to work, matriceIncidentaArcs needs to be a 2d array (preferable a vector<vector>, not an int[][].
But this is just one of the many problems in your code.
The pointers and double pointers you declare, screams of bugs that will explode your code when you get as far as running it. Don't use pointers until you know what you're doing. Instead, start learing std::vector or std::array
By doing that, you will be able to use ranged-based for loops instead of the raw for-loops you are mis-using too.

How to fix addition/multiplication table errors?

I am having troubles with some of the inputs for my addition/multiplication table, and am hoping to find some help towards fixing this. I will begin by posting what I have for the program.
The code is as follows :
#include <iostream>
using namespace std;
void die() {
cout << "BAD INPUT!" << endl;
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
cout << "Choose:\n";
cout << "1. Addition Table\n";
cout << "2. Times Table\n";
cin >> choice;
if (!cin) die();
if (choice != ADD and choice != MULTIPLY) die();
cout << "Please enter the smallest number on the table:\n";
cin >> min;
if (!cin) die();
cout << "Please enter the largest number on the table:\n";
cin >> max;
if (!cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
cout << '+';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
cout << '\n';
}
}
if (choice == MULTIPLY) {
for (int i = min; i <= max; i++) {
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
cout << '\n';
}
}
}
Now, here are the mistakes that I am getting from this code that I cannot seem to resolve. First, when doing the MUlTIPLY table with min = 1, max = 1, I am getting:
X 1
when I should be getting (I believe)
X 1
1 1
Secondly, while doing the MULTIPLY table with min = 1, max = 12, I am getting:
X 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
when I should be getting
X 1 2 3 4 ... 12
1 1 2 3 4 ... 12
2 2 4 6 8 ... 24
3 3 6 9 12 ... 36
And finally, when using the ADD table with min = 21, max = 40, I cannot post all of the code since it is such a mess, but basically the columns/rows are as follows:
+ 21 22 23 24 25 ...
5
1
6
2
7
3
8
When obviously, the code should output the rows and columns to be 21 - 40 evenly. As you can see in the last example, my rows are outputting properly, but somehow my columns are a complete, garbled mess.
I have been sitting and staring at this code for awhile, and can't seem to fix these issues at hand. Can anyone help lead me in the right direction? I really appreciate any help and hints :)
Check this out. Might not be fully optimized, but works
if (choice == ADD) {
cout << '+';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i + j << '\t';
}
}
}
if (choice == MULTIPLY) {
cout << 'X';
for (int i = min; i <= max; i++) {
cout << '\t' << i;
}
for (int i = min; i <= max; i++) {
cout << '\n' << i << '\t';
for (int j = min; j <= max; j++) {
cout << i * j << '\t';
}
}
}
See output here.
#include <iostream>
#include <iomanip>
#include <cstdio>
void die()
{
std::cout << "BAD INPUT!" << "\n";
exit(1);
}
int main() {
const int ADD = 1;
const int MULTIPLY = 2;
const int MAX_SIZE = 20;
int choice = 0, min = 0, max = 0;
std::cout << "Choose:\n";
std::cout << "1. Addition Table\n";
std::cout << "2. Times Table\n";
std::cin >> choice;
if (!std::cin) die();
if (choice != ADD and choice != MULTIPLY) die();
std::cout << "Please enter the smallest number on the table:\n";
std::cin >> min;
if (!std::cin) die();
std::cout << "Please enter the largest number on the table:\n";
std::cin >> max;
if (!std::cin) die();
if (min > max) die();
if (max - min >= MAX_SIZE) die();
if (choice == ADD) {
for (int i = 0; i <= max; i++) {
if (i == 0)
printf(" +");
else
printf("%3d", i);
printf(" ");
for (int j = min; j <= max; j++) {
printf("%3d ", i + j);
}
printf("\n");
}
}
if (choice == MULTIPLY) {
/* for printing header of the multiplication table */
std::cout << "X\t";
for (int j = min; j <= max; j++) {
std::cout << min * j << "\t";
}
std::cout << "\n";
/* for printing rest of the table */
for (int i = min; i <= max; i++) {
std::cout << i << "\t";
for (int j = min; j <= max; j++) {
std::cout << i * j << '\t';
}
std::cout << '\n';
}
}
}
The crucial mistake in your code for multiplication was that, you were trying to print (max - min + 1) + 1 rows in total, the extra +1 for the header. While your code was printing the first row as header and then starting directly with the second row.
Your code for addition table was correct, but 21 to 40 with tab character in between was too taking too much space for a typical laptop screen, not to say the output won't be pretty.
On my system, the output of tput lines and tput cols was 38 and 144 resp.
which wasn't sufficient for your code.
you can format the output with printf using printf fixed width output.
Considering you are not much familiar with C++, I would like to state that
using the std namespace as default namespace will work for this program, but when you working with larger projects, you should always prefix it.
I haven't enough reputation to add comment
if I were, I was commenting these lines
if (i == min) {
cout << 'X';
else
cout << i;
cout << '\t';

power function no scope in function

#include<iostream>//Pls note:Only header allowed...
As this is the C++ i dont think so any other header is needed for that math function.
using namespace std;
int comparator(int audience[][2], int index1, int index2) {
int b1, e1;
int b2, e2;
b1 = audience[index1][1];
e1 = audience[index1][2];
b2 = audience[index2][1];
e2 = audience[index2][2];
double re1;
re1 = pow(b1, e1);
cout << re1 << endl;
double re2 = pow(b2, e2);
cout << re2 << endl;
if (re1 == re2)
{
return 0;
}
if (re1 > re2)
{
return -1;
}
if (re1 < re2)
{
return 1;
}
}
//Nothing has to be done with the rest of the two functions.
void sorting(int audience[][2], int N, int &i_index, int &j_index)
{
int i, j, temp;
for (i = 0; i<N - 1; i++)
{
if (audience[i][2] < audience[i + 1][2])
continue;
else
i_index = i;
break;
}
for (i = N; i > 1; i++)
{
if (audience[i][2]>audience[i - 1][2])
continue;
else
j_index = i;
break;
}
for (i = i_index + 1; i < j_index - 1; i++)
{
min = audience[i_index + 1][2];
for (i = )
if (audience[i_index][1] > audience[i_index + 1][1])
{
temp = audience[i_index + 1][1];
audience[i_index + 1][1] = audience[i_index][1];
audience[i_index][1] = temp;
}
}
for (i = i_index + 1; i <= j_index - 1; i++)
{
min = audience[i][2];
for (j = i_index + 2; j <= j_index - 1; j++)
{
if (min > audience[j][2])
{
temp = audience[i_index + 2][2];
audience[i_index + 1][2] = audience[i_index][2];
audience[i_index][2] = temp;
}
}
}
}
void merge(int audience[][2], int mergedarray[][2], int N, int i_index, int j_index)
{
}
int main()
{
int audience[100][2], mergedmarks[100][2];
int i, N;
int index1 = 0, index2 = 0;
int comp_result;
cout << "Enter the value of N : ";
cin >> N; // Enter size of the table
cout << "Enter the base and exponent for " << N << "rows " << endl;
for (i = 0; i < N; i++)
cin >> audience[i][0] >> audience[i][1]; //Enter numbers in the table
cout << endl << "Checking Function 1: Compare ecodes for 5 index pairs" << endl;
for (i = 0; i < 5; i++)
{
cout << "\nEnter indices of row1 and row2 that you want to compare: ";
cin >> index1 >> index2;
if (index1 < 0 || index2 < 0 || index1 >= N || index2 >= N)
continue;
comp_result = comparator(audience, index1, index2);
if (comp_result == -1)
cout << "ecode of index 1 is greater than ecode of index2" << endl;
else if (comp_result == 1)
cout << "ecode of index 1 is less than ecode of index2" << endl;
else if (comp_result == 0)
cout << "ecode of index 1 is equal to ecode of index2" << endl;
}
cout << endl;
int index_i = 0, index_j = N;
sorting(audience, N, index_i, index_j);
cout << "Checking Function 2: Printing sorted array " << endl;
for (i = 0; i < N; i++)
cout << audience[i][0] << " " << audience[i][1] << endl;
cout << endl;
cout << "index i: " << index_i << "\nindex j: " << index_j << endl;
cout << endl << "Checking Function 3: Printing Merged Array " << endl;
merge(audience, mergedmarks, N, index_i, index_j);
int merge_array_size = index_i + (N - (index_j + 1));
for (i = 0; i < N; i++)
cout << mergedmarks[i][0] << " " << mergedmarks[i][1] << endl;
cout << endl;
return 0;
}
This is the whole problem. I have to still edit the merge function. That is the whole problem.This is all.
You need to include the pow header, which is math.h, in order to use it.
add at the beginning of your file:
#include <math.h>
#include <iostream>
using namespace std;
POW is declared in math.h header file so use
#include<math.h>

C++: How do I make this shape from this code?

I am trying to make this shape from the code below. I'm confused as to how to make it print the 2nd row, second to last star without it skipping and printing the extra space before printing the star. Once that is figured out would the bottom half, when the stars expands back out, would the code be similar to the top half? I have tried a couple combinations of code between c and r but I have been stuck with what I currently.
---------------------- //row 0
* *| //row 1
* * * *| //row 2
* * * * * *|
* * * * * * * *|
* * * * * * * * * *|
* * * * * * * * * * *|
* * * * * * * * * *|
* * * * * * * *|
* * * * * *|
* * * *|
* *|
----------------------
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
for (int c = 0; c < num; ++c) //inner loop/columns
{
if (r == 0) cout << "--"; //top of square
else if (c >= r + r - c && c < num - 1)
cout << " ";
//else if (c == num - 1) cout << "*|";
else if (r == num - 1) cout << "--"; //bottom of square
else if (c == num - 1) cout << "*|"; //right side of square
else if (r > c) cout << "* ";
}
cout << endl;
}
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
I just took two variables left=0 & right=num-1 and increased left & decreased right till r<=num/2, after that i reversed the process,when the col <= left or col >=right I printed *.
I hope it will be easy to understand.
Here is the code:
#include <iostream>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a)
{
cin >> num;
if (num < 38 && num > 0 && num % 2 == 1)
{
cout << "Thank you!" << endl << endl;
int left=0,right=num-1;
//for printing top line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
for (int r = 0; r < num; ++r) //outer loop/rows
{
//printing columns
for(int c = 0; c < num; c++)
{
if(c <= left || c >= right)
cout<<"* ";
else
cout<<" ";
}
if(r >= num/2) //checking for half of the rows
{
left--;right++;
}
else
{
left++;right--;
}
cout<<"|"<<endl;
}
//for printing last additional line
for(int i = 0; i < num; i++) cout<<"- ";
cout<<"-"<<endl;
break;
}
else cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
This approach does it the math way.
Furthermore it draws a full frame with plus-chars at the edges.
Give it a try.
#include <iostream>
#include <cmath>
using std::cout; using std::cin; using std::endl;
int main() {
cout << "Enter a positive odd number less than 40: ";
int num = 0;
int z = 1;
for (int a = 0; a < 3; ++a) {
cin >> num;
if (num < 40 && num > 0 && num % 2 == 1) {
cout << "Thank you!" << endl << endl;
int center = ceil(num / 2.0);
for (int r = 0; r <= num+1; ++r) { //outer loop/rows
for (int c = 0; c <= num+1; ++c) { //inner loop/columns
if (r == 0 || r == num+1) {
if (c == 0 || c == num+1)
cout << "+"; // corner
else
//top or botton of square between corners
if (c == center)
cout << "-";
else
cout << "--";
}
else if (c == 0 || c == num+1) {
cout << "|"; // left or right frame
} else {
// inner part
if ((center-std::abs(center-r)) >= center-std::abs(center-c))
if (c < center)
cout << "* ";
else if (c > center)
cout << " *";
else
cout << "*";
else
if (c == center)
cout << " ";
else
cout << " ";
}
}
cout << endl;
}
} else
cout << "Please enter a positve odd number that is less than 40!" << endl;
}
cout << endl;
}
Just another way (with some more user input checking):
#include <iostream>
#include <string>
#include <limits>
#include <sstream>
using std::cout;
using std::cin;
using std::string;
const auto ssmax = std::numeric_limits<std::streamsize>::max();
const int max_dim = 40;
const int max_iter = 3;
int main() {
cout << "Enter a positive odd number less than " << max_dim << ": ";
int num = 0, counter = 0;
while ( counter < max_iter ) {
cin >> num;
if ( cin.eof() )
break;
if ( cin.fail() ) {
cout << "Please, enter a number!\n";
cin.clear();
cin.ignore(ssmax,'\n');
}
if ( num < max_dim && num > 0 && num % 2 ) {
cout << "Thank you!\n\n";
//top line
string line(num * 2, '-');
cout << line << '\n';
for ( int r = 0, border = num - 1; r < num; ++r ) {
cout << '*';
for ( int c = 1; c < num; ++c ) {
if ( (c > r && c < border) || (c < r && c > border) )
cout << " ";
else
cout << " *";
}
// right border
cout << "|" << '\n';
--border;
}
//bottom line
cout << line << '\n';
++counter;
} else {
cout << "Please, enter a positive odd number that is less than 40!\n";
}
}
cout << std::endl;
}
Or my favorite:
// top line
string line = string(num * 2, '-') + '\n';
cout << line;
// inside lines
int r = 0, border = ( num - 1 ) * 2;
string inside = string(border + 1, ' ') + "|\n";
// top
while ( r < border ) {
inside[r] = '*';
inside[border] = '*';
r += 2;
border -= 2;
cout << inside;
}
// center line
inside[r] = '*';
cout << inside;
// bottom
while ( border > 0 ) {
inside[r] = ' ';
inside[border] = ' ';
r += 2;
border -= 2;
cout << inside;
}
//bottom line
cout << line;

Xcode 7 'Function Definition is not allowed here'

My code is below. I have tried all different options. I am still learning C++ through classes but have reached an impasse. Xcode says error are in the lines there are three straight * marks. Ex: ***code* Any advice would be great and looking for ways to understand arrays better.
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;
int board [7] [7];
void initBoard (int array1[7][7]) {
srand(time(0));
string position[7][7];
int moves = rand() % 8 + 5;
int r = 0;
int c = 0;
for (c = 0; c < 7; c++) {
if (c == 0 || c == 6) {
for (r = 0; r < 7; r++) {
position[r][c] = " ";
}
}
else {
for (r = 0; r < 7; r++) {
if (r == 0 || r == 6) {
position[r][c] = " ";
}
else {
position[r][c] = "_ ";
}
}
}
}
***void plantBombs (int array2[7][7]) {***
r = 0;
c = 0;
int k = (rand() % 5) + 1;
int d = ((rand() % 111) % 5) + 1;
int numBombs = rand() % 4 + 3;
int bombR[numBombs];
int bombC[numBombs];
int i = 0;
while (i < numBombs){
bombR[i] = (rand() % 71) % 5 + 1;
bombC[i] = (rand() % 123) % 5 + 1;
while (bombR[i] == k && bombC[i] == d) {
bombR[i] = (rand() % 97) % 5 + 1;
bombC[i] = (rand() % 79) % 5 + 1;
}
while ((i > 0) && (bombR[i-1] == bombR[i]) && (bombC[i-1] == bombC[i])){
bombR[i] = (rand() % 213) % 5 + 1;
bombC[i] = (rand() % 857) % 5 + 1;
}
position[bombR[i]][bombC[i]] = "* ";
i = i + 1;
}
}
***void placeYou (int array2[7][7]){***
position[k][d] = "U ";
}
***void movePlayer (int arrary3[7][7]){***
while (moves != 0) {
cout << "SURVIVE " << moves << " MORE MOVES!" << endl;
for (c = 0; c < 7; c++) {
for (r = 0; r < 7; r++) {
cout << position[r][c];
}
cout << endl;
}
cout << "Enter direction (N/S/E/W): ";
string direction;
cin >> direction;
//if (direction == "N")
if (direction == "N"){
if (position[k][d-1] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d-1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d-1] = "U ";
d = d - 1;
}
}
if (direction == "E"){
if (position[k+1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k+1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k+1][d] = "U ";
k = k + 1;
}
}
if (direction == "S"){
if (position[k][d+1] == " ") {
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k][d+1] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k][d+1] = "U ";
d = d + 1;
}
}
if (direction == "W"){
if (position[k-1][d] == " "){
cout << "You fell off the board" << endl;
cout << "***YOU LOSE***" << endl;
break;
}
else if (position[k-1][d] == "* "){
cout << "BANG! YOUR DEAD!" << endl;
cout << "***YOU LOSE!***";
break;
}
else {
position[k][d] = "* ";
position[k-1][d] = "U ";
k = k - 1;
}
moves = moves - 1;
}
if (moves == 0){
cout << "***YOU WIN***";
}
else {
cout << "";
}
}
}
}
int main()
{
initBoard(board);
***plantBombs(board);
placeYou(board);
movePlayer(board);***
return 0;
}