C++ Check if number is even or odd - c++

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;
}

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.

Exception handling in c++ how do I implement it here

I just created a program in c++ about adding matrices but I just dont know how to exception handle the part where to choose a number from 1 to 10 so that the user can only choose a number from 1 to 10 and if he puts a wrong input in it shows an error message and is asked to input a number again
#include <iostream>
using namespace std;
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
void matrix(string s) {
cout << "Enter number of rows (between 1 and 10): ";
cin >> r;
cout << "Enter number of columns (between 1 and 10): ";
cin >> c;
}
void storeValues(string s) {
cout << endl << "Enter elements of " << s << " matrix:" << " " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << "Enter element " << i + 1 << j + 1 << " : ";
if (s == "1st") {
cin >> a[i][j];
}
else
cin >> b[i][j];
}
}
void addMatrices() {
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];
}
void displayResult() {
cout << endl << "Sum of two matrix is: " << endl;
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if (j == c - 1)
cout << endl;
}
}
int main()
{
string s = "1st";
matrix(s);
storeValues(s);
s = "2nd";
storeValues(s);
addMatrices();
displayResult();
return 0;
}
You could easily make a function that handles bounded input for you, something like:
int bounded_input(int lower, int upper, std::string message) {
int r;
bool valid = false;
cout << message << " (between " << lower << " and " << upper << "): ";
do {
cin >> r; // NOTE: You will want to check for invalid input like "three" ...
if (r < lower || r > upper) {
cout << "Invalid, please enter between " << lower << " and " << upper << ":";
} else {
valid = true;
}
} while (!valid);
return r;
}
Then you can simply call this for all of your bounds:
int row_bound = bounded_input(1, 10, "Enter row bounds");

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 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;
}
}