Matrix subtraction endless display issue - c++

I've been working on a program to display the result of the subtraction of two matrices. The program allows the user to input the size of the matrices, then prompts the user to input values for these two matrices. Lastly, the program is supposed to display these two matrices individually, then display the result of the subtraction of the two.
Currently when the code is ran, the output is just an endless display of numbers. I cannot find what is creating this issue. I would appreciate any input as to what is causing this issue.
Thanks!
using namespace std;
#include <iostream>
#include <conio.h>
int main(){
int i = 0, j = 0, n=0, a[10][10], b[10][10], c[10][10];
bool positive = false;
cout << "Enter the size of the two - dimensional array: ";
cin >> n;
while (!positive){
if (n >= 0){
positive = true;
}
else {
cout << "Please enter a positive number for the size of the matrix.";
cin >> n;
}
}
cout << "Enter the values of the elements of array A" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cin >> a[i][j];
}
}
cout << "Enter the values of the elements of array B" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cin >> b[i][j];
}
}
cout << "Matrix A:" << endl;
for (i = 0; i < n; i++){
for (j = 0; i < n; j++){
cout << a[i][j] << " ";
}
}
cout << "Matrix B:" << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << b[i][j] << " ";
}
cout << "\n";
}
for (i = 0; i < n; i++){
cout << endl;
for (j = 0; j < n; j++){
c[i][j] = a[i][j] - b[i][j];
}
cout << "\n";
}
cout << "Matrix A - Matrix B: " << endl;
for (i = 0; i < n; i++){
for (j = 0; j < n; j++){
cout << c[i][j] << " ";
}
cout << "\n";
}
return 0;
}

Probably this line is causing the trouble?
cout << "Matrix A:" << endl;
for (i = 0; i < n; i++){
for (j = 0; i < n; j++){
cout << a[i][j] << " ";
}
}
Change the for-loop for j (i.e, j < n). See if that helps..

Related

C++ Nested Loops: n*n grid

I need help with this assignment.
I already tried something and I can include the code that I wrote so far.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int i, j, n;
cout << "Please enter the positive integer:";
cin >> n;
if (n < 0) {
cout << "\nEntered integer is not positive! Please enter the positive integer:";
cin >> n;
}
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n) {
cout << setw(n) << "*";
}
else {
cout << setw(n) << " ";
}
}
cout << endl << "\n";
}
return 0;
}
Thanks in advance!
There are several issues in your implementation:
if (n < 0) {
cout << "\nEntered integer is not positive! Please enter the positive integer:";
cin >> n;
}
The above condition will validate the user input only once. If the user enter a negative number more than once, it will not be checked.
cout << endl << "\n";
This will cause two end of lines to be printed, which is not needed.
Here is a working program which does the required job:
int main()
{
int n;
cout << "Please enter the positive integer: ";
cin >> n;
while (n < 0) {
cout << "\nEntered integer is not positive! Please enter the positive integer: ";
cin >> n;
}
for (int i = 0; i <= (n*n); i++) {
for (int j = 0; j <= (n*n); j++) {
if (i == 0 || (i % n == 0) || (j == 0) || (j % n == 0)) {
cout << setw(2) << "*";
} else {
cout << setw(2) << " ";
}
}
cout << '\n';
}
return 0;
}
Here is the output from the program:
Assuming a 1x1 grid looks like this:
****
* *
* *
****
e.g., is not also dependent on the number inputted, I would use the following:
for (int i = 0; i < n * 3 + 1; ++i) {
cout << "*"; //First row with stars
}
cout << endl;
for (int i = 0; i < n; ++i) {
cout << "*"; //First column with stars
for (int j = 0; j < n; ++j) {
cout << " *"; //Empty box and closing star
}
cout << endl;
cout << "*"; //First column with stars
for (int j = 0; j < n; ++j) {
cout << " *"; //Empty box and closing star
}
cout << endl;
for (int j = 0; j < n * 3 + 1; ++j) {
cout << "*"; //Bottom row of box with stars
}
cout << endl;
}
This code is not tested but just quickly typed, it might not work, so be careful using it. Let me know if there are some problems.

Limiting the inputs and again asking the user for the right range

I'm stuck on this program: I have to limit the input but the program just totally ignore the if statement if i enter a value greater or equal to 100 it should
display an error but continue to ask user for input and run
Here is my code:
#include<iostream>
using namespace std;
int main()
{
int a[4][4], big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
if (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
else
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}
I've added a while to repeat the check until the number falls below 100. Please note the line "int a[4][4],.." will overrun if you exceed 4 rows * 4 columns. I suggest moving its initialization to a lower position when rows and columns are known.
Try this :
#include<iostream>
using namespace std;
int main()
{
int big1, n, m, i, j, loc1, loc2;
cout << "Enter no of rows and columns:";
cin >> m >> n;
cout << "Enter the array:\n";
while (n > 100 || m>100 )
{
cout << "Error! number should in range of (1 to 99)." << endl;
cout << "Enter the number again: ";
cin >> m >> n;
}
int a[m][n];
//else redundant
for (i = 0; i < m; i++)
{
for (j = 0; j < n; ++j)
{
cin >> a[i][j];
}
}
cout << endl << "Entered Matrix: " << endl;
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cout << " " << a[i][j];
if (j == n - 1)
cout << endl << endl;
}
big1 = a[0][0];
loc1 = 0;
loc2 = 0;
for (i = 0; i < m; ++i)
{
for (j = 0; j<n; ++j)
{
for (int i = 0; i<4; i++)
for (int j = 0; j<4; j++)
if (a[i][j]>big1)
{
big1 = a[i][j];
loc1 = i;
loc2 = j;
}
}
}
cout << "\nLargest number:" << big1 << endl;
cout << "The position that had the largest number is in " << " Row " << loc1 << " " << "Column " << loc2 << endl;
system("pause");
return 0;
}

How to read and write a 3D array from text file using c++?

I'm currently making a program for school which involves a booking system for seats, and I'm trying to implement a system where seats are saved after every run of the program, and as soon as the program opens again, the current seats taken or available are shown.
Here is my code currently:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int seats[6][23], ticket_count, h, l;
char column;
column = 'A';
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 23; j++){
seats[i][j] = 0;
}
}
for (int i = 0; i < 6; i++) {
cout << column << " ";
column++;
for (int j = 0; j < 23; j++){
cout << seats[i][j];
cout << " ";
}
cout << endl;
}
cout << "\nEnter number of tickets to purchase: ";
cin >> ticket_count;
if (ticket_count > 23)
{
cout << "You can only purchase a maximum of 23 tickets.\n";
}
else if (ticket_count < 1)
{
cout << "You have to buy a minimum of 1 ticket.\n";
}
else
{
for (int q = 0; q < ticket_count; q++)
{
cout << "Ticket " << q + 1 << endl;
cout << "Row (1,2,3,4,5,6): ";
cin >> h;
cout << "Seat (1-23): ";
cin >> l;
h = h - 1;
l = l - 1;
seats[h][l] = 1;
column = 'A';
}
for (int i = 0; i < 6; i++) {
cout << column << " ";
column++;
for (int j = 0; j < 23; j++){
cout << seats[i][j];
cout << " ";
}
cout << endl;
}
}
ofstream myfile;
myfile.open("seats.txt");
myfile << seats;
myfile.close();
return 0;
}`
However, when I open the text file, it gives me a random hexadecimal string. I could really use some assistance. Thank you!

how can i make my input no. be like 1010 not 1 "\n" 0 "\n" 1 "\n" 0 "\n"

#include <iostream>
using namespace std;
int main(){
int array_size, array[100];
cout << "Enter length of number you want to input ";
cin >> array_size;
cout << "Enter the inputs" << endl;
for (int i = 0; i < array_size; i++){
cin >> array[i];
if (array[i] == 0){
array[i] = 1;
}else{
array[i] = 0;
}
}
cout << "output" << endl;
for (int i = 0; i < array_size; i++){
cout << array[i];
}
cout << endl;
system("PAUSE");
return 0;
}
Read the input to a string and parse the numbers from that string.
std::string input;
std::cin >> input;
for (int i = 0; i < array_size; i++){
if (input.at(i) == '0') {
array[i] = 1;
} else {
array[i] = 0;
}
}

Coding hollow square in c++ with nested for loop and if statement

So for a lab project the goal is to print out shapes using a "*."
The four basic shapes are a square, two downwards pointing right triangles, and a hollow square. Everything seems to be working fine for any integer I input but I'm not entirely sure I'm doing the loop for the hollow square correctly and would like it if someone could give me some advice.
#include <iostream>
using std::cout; using std::endl; using std::cin;
int main()
{
cout << "Input arbitrary integer ";
int n;
cin >> n;
cout << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < n; j++)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 2; j <= i; j++)
{
cout << " ";
}
for (int j = n; j >= i; j--)
{
cout << "*";
}
cout << endl;
}
cout << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n; j++)
{
if (i == 1)
cout << "*";
else if (j == 1)
cout << "*";
else if (i == n)
cout << "*";
else if (j == n)
cout << "*";
else
cout << " ";
}
cout << endl;
}
}