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.
Related
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;
}
I have created program in C++ that checks if entered numbers are: even and positive,even and negative,odd and positive or odd and negative!
Is there any better solution to check this?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
system("color f0");
int n = 0, A[1337], B[1337];
cout << "Enter number of array members: " << endl;
cin >> n;
cout << "Enter numbers : \n";
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < n; i++)
B[i] = A[i];
for (int i = 0; i < n; i++)
{
if (B[i] > 0 && B[i] % 2 == 0)
cout << B[i] << " pp" << endl;
if (B[i] > 0 && B[i] % 2 != 0)
cout << B[i] << " pn" << endl;
if (B[i] < 0 && B[i] % 2 != 0)
cout << B[i] << " np" << endl;
if (B[i] < 0 && B[i] % 2 == 0)
cout << B[i] << " nn" << endl;
}
cout << endl;
system("pause");
return 0;
}
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
system("color f0");
int n = 0, A[1337];
cout << "Enter number of array members: " << endl;
cin >> n;
//Make sure n is not bigger than your arrays
if( n > 1337 ) n=1337;
cout << "Enter numbers : \n";
for (int i = 0; i < n; i++)
cin >> A[i];
for (int i = 0; i < n; i++)
{
if( A[i] == 0 )
{
cout << A[i] << " is 0" << endl;
}
else
{
cout << A[i] << " " << (A[i] < 0? "n": "p") << (A[i]%2? "p":"n");
}
}
cout << endl;
system("pause");
return 0;
}
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!
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..
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;
}
}