how to make this number shape? - c++

Write a program to display the below pattern with n rows, where n is
in the range between 1 and 100. The variable n should be entered by
the user. If the user input is between 1 and 100 then output the
pyramid as given below, otherwise prompt the user to enter n again.
Here is the sample output: Enter the number of rows: 6
1
2 3
3 4 5
4 5 6 7
5 6 7 8 9
6 7 8 9 10 11
(enter image description here)
this is my code it shows a close answer but not correct.
int num=1 , counter=1;
cout << "Enter the number of rows: " ;
cin>>num;
for(int i=0;i<=num;i++)
{
for(int j=0;j<=i;j++)
{
cout<<counter<<" ";
counter++;
}
cout<<endl;
}

int num = 1, counter = 1, temp = 1;
cout << "Enter the number of rows: ";
cin >> num;
for (int i = 0; i < num; i++)
{
for (int j = 0; j <= i; j++)
{
cout << temp << " ";
temp++;
}
counter++;
temp = counter;
cout << endl;
}
The variable temp serves as a counter for the rows, meanwhile the variable counter is responsible for the starter numbers in the first column.

you should either set counter in each run of outer loop, right before entering the inner one with something like counter = i+1, the way you wrote this code value of this variable carries over to the next iteration and keeps going only upwards.
Alternate solution would be to print j instead and work on inner loop, it should then start with i and the first number that wouldn't qualify would be 2*i this way for i equal 2 the sequence would be 2 3,
either way you should also rework your outer loop, as right now it starts with 0 and ends with num meaning it goes through num+1 iterations

int rows, i, j = 0, number = 0, counter = 0;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++)
{
while (j != 1 * i)
{
if (counter <= rows)
{
cout << i+j << " ";
counter++;
}
++j;
}
number=counter=j=0;
cout << endl;
}

Related

Reverse right angle triangle pattern with numbers and asterisks using for-loops in C++

first time posting here, I'd like help with getting the output of the code upside down while still using the for commands, below is the best I can put in a clarification.
Desired Output: Actual Output:
123456 1*****
12345* 12****
1234** 123***
123*** 1234**
12**** 12345*
1***** 123456
Code:
#include <iostream>
using namespace std;
int main() {
int num, row, integ;
cout << "Please enter size: ";
cin >> integ;
for (row = 1; row <= integ; row++) {
for (num = 1; num <= row; num++) {
cout << num;
}
for (; num <= integ; num++) {
cout << "*";
}
cout << endl;
}
}
first time answering here :).
change num <= row to num <= integ - row + 1
Let's try to understand what we require.
We want to display number followed by stars. The numbers should decrease in each iteration and stars should increase.
Iteration-1: display 1 to integ - 0 and 0 asterisk
Iteration-2: display 1 to integ - 1 and 1 asterisk
Iteration-3: display 1 to integ - 2 and 2 asterisk
Iteration-4: display 1 to integ - 3 and 3 asterisk
...
and so on.
As you can see, in each iteration we need to display value from 1 to (integ - x) and x asterisk, where x will starts from zero and keep on increasing.
To put this logic in code:
int main() {
int integ;
cout << "Please enter size: ";
cin >> integ;
for(int i = 0; i < integ; ++i) {
for(int j = 0; j < integ; ++j) {
if(j < (integ-i)) {
cout << j+1;
} else {
cout << '*';
}
}
std::cout << '\n';
}
}
And here is the output:
123456
12345*
1234**
123***
12****
1*****
Hope it helps.

How can i write an array starting from right to left in C++

I don't want to reverse or shift the array. What I want is to write the array from right to left.
I did something like
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
cin >> arr[i];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
}
cout << endl;
}
and on the output screen I see this
enter a number : 5
5 X X X X
enter a number : 4
5 4 X X X
enter a number : 3
5 4 3 X X
enter a number : 2
5 4 3 2 X
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
but i want to see this
enter a number : 5
X X X X 5
enter a number : 4
X X X 5 4
enter a number : 3
X X 5 4 3
enter a number : 2
X 5 4 3 2
enter a number : 1
5 4 3 2 1
Press any key to continue . . .
how can I do that?
I will be glad if you help.
Just change the inner for loop for example the following way
int j = 5;
for ( ; j != 0 && arr[j-1] == 0; --j )
{
std::cout << 'X' << ' ';
}
for ( int k = 0; k != j; k++ )
{
std::cout << arr[k] << ' ';
}
I think you are trying to write into the last index each time, and then move each number backwards in the array?
To write into the last index use cin >> arr[4].
Then After printing out the array copy each value into the previous index. Make sure that your copy only when the J index is less than 4
int arr[5] = {0};
for (int i = 0; i < 5; i++)
{
cout << "enter a number : ";
//write into the last position
cin >> arr[4];
for (int j = 0; j < 5; j++)
{
if (arr[j] != 0)
{
cout << arr[j] << " ";
}
else
cout << "X ";
//don't go out of bounds. This should move each number to the previous index
if(j<4){
arr[j]=arr[j+1];
}
}
cout << endl;
}

How to cycle through 2 arrays and determine if values in each array are the same?

I am programming a guessing game where the user will define the first array to scale from 1 to whatever number inputed. I have a function that will randomise the numbers, then the second array will be the user input guesses. I am trying to make a for loop that will cycle through both arrays and print "O" if array values match, and "X" if they do not. It looks like the first iteration of the loop works and prints "O" but then prints a lot of X's after that.
#include <iostream>
using namespace std;
void createArray() {
int n;
int counter = 0;
cout << "Enter total number: ";
cin >> n;
// for loop to generate array to scale from 1 -
user input.
int *arr1 = new int[n];
for (int i = 1; i < n + 1; i++){
arr1[i] = i;
// Counter is used to track the size of array
counter++;
}
// for loop used to print the values of array (just for development and my reference)
for (int i = 1; i < n + 1; i++){
cout <<"[" << arr1[i] << "]";
}
cout << endl;
cout << counter << endl << "Number Guessing" << endl;
cout << "Enter " << counter << " digits (1-" << counter << ")" << endl;
// initialize user input array to be the same size as first array
int arr2[counter];
for (int i =0; i < counter; i++){
cin >> arr2[i];
}
cout << endl;
// here I am trying to cycle through both arrays and print "O" if the values match in each array.
for (int i = 0; i < n; i++){
arr1[i] = i;
bool correct = true;
for (int j = 0; j < n; j++){
arr2[j] = j;
if (arr1[i] != arr2[j]){
correct = false;
cout << "X";
}
if (correct){
cout << "O";
}
}
}
cout << endl;
}
int main(){
createArray();
}
This is the output:
Enter total number: 6
[1][2][3][4][5][6]
Number Guessing Enter 6 digits (1-6)
1 2 3 4 5 6
OXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Your loops are not comparing the arrays side by side but rather every element in array 1 is being compared to every element in array 2.
Since both arrays are reset by arr[i]=i & arr2[j]=j then they both are {0,1,2,3,4,5}.
The first innerloop will net a 'O' if(0==0) where i=0 & j=0, the next 5 innerloops if(0==1..5) where i=0 & j=1..5, are an 'X'.
The problem is on the next outerloop. Correct is reset to true by the outerloops second pass of i=1, but immediately the innerloop tests if(1==0) where i=1 & j=0, which is false and thus sets Correct to false.
So on the second pass thru the innerloop if(1==1) is true where i=1 & j=1, BUT there is no statement in the inner loop to set **Correct back to true.
Also, where as the X is printed as the result of the equality test, the O is outputted only when Correct is still true, which it is not. In fact, for every outloop after the first one, there will be an innerloop equality test which sets Correct to false before any a subsequent true equality tests.
The results are one 'O' followed by a 30 'X'. One 'X' for every false; 6 outerloops, and foreach, 5 of the six innerloops will be false. Five of the 'O's will simply not print because the 'O' does not print as a result of the equality test.
You dont need two loops
for (int i = 0; i <= n; ++i) {
if (arr1[i] == arr2[i]) {
std::cout << "0";
}
else {
std::cout << "X";
}
}
You should put your expected output for us to know what you want

How to check how many times a number has been entered

I'm trying to solve one of the questions on a task sheet I've been received, to help me further in my understanding of C++ code from my class.
The question is (and I quote):
Write a program that:
Asks the user to enter 10 numbers between 1 and 5 into an array and displays the array on screen
Creates a second array of size 5 and fills it with zeros
Counts how many 1s, 2s, , … 5s have been entered into the first array and stores this number in the second array.
Displays the second array as shown in the example below.
The problem is how to go about checking how many times a number was entered. I was thinking of a for loop, but the way I wrote it is fundamentally incorrect, so I find myself struggling to see the mistake I am having. Perhaps I am missing something simple? Any help would be great.
Here is my (terrible) for loop attempt, so you can see my error.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int input[10];
const int MAX_NO = 5;
int COUNT[5] = { 0,0,0,0,0 };
int count = 10;
for (int i = 0; i < count; i++)
{
cout << "Please enter a number for value " << i + 1 << " :";
cin >> input[i];
while (input[i] < 1 || input[i] > 5)
{
cout << "Error: Enter another number between 1 and 5: ";
cin >> input[i];
}
}
cout << endl << "You entered ";
for (int i = 0; i < count; i++)
{
cout << input[i] << " ";
}
cout << "\n";
// show how many times 1 number appears
for (int i = 1; i <= 5; i++)
{
if (input[i] == i)
{
COUNT[i]++;
}
}
for (int i = 0; i < MAX_NO; i++)
{
cout << i + 1 << " appears " << COUNT[i]
<< " times in the input" << endl;
}
cout << endl;
system("pause");
return 0;
}
Put
COUNT[ input[i]-1 ]++;
in your first loop (after validation). Once you do that, you don't need a second loop to tally up the results.
This works from the inside out by first getting what input[i] is, then using it to modify the (input[i]-1)'th location in the COUNT array. If the user enters 4 on the first run of the loop, then i == 0 and input[i] == 4. Since arrays are 0-based, it will increment COUNT[input[i]-1] which in this case is COUNT[4-1] == COUNT[3].
After your initial loop runs the number of 1's will be in COUNT[0], the number of 2's will be in COUNT[1] and so on.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
//declare a constant values
int input[10];
int count = 10; //all constant MUST be in capital letters
//second array filled with zeros
const int MAX_NO = 5;
int COUNT[5] = { 0, 0, 0, 0, 0 };
//ask user for 10 input values
for (int i = 0; i < count; i++)
{
cout << "Please enter a number for value " << i + 1 << " :";
cin >> input[i];
//check if input numbers are between 1 and 5 inclusive
while (input[i] < 1 || input[i] > 5)
{
cout << "Error: Enter another number between 1 and 5: ";
cin >> input[i];
}
/* show how many times 1 number appears.
this section should be in the main loop which would enable the program to check how many times a
number is entered so that it is stored in the second array. changed i to secondCount because this is the counting index of the second array not the first which you've called i (one of the reason you'd all zero as output when u ran your code)*/
for (int secondCount = 1; secondCount <= MAX_NO; secondCount++)
{
if (input[i] == secondCount)
{
COUNT[secondCount-1]+= 1; //use minus 1 from i and increment. += 1 is the same as COUNT++
}
}
}
//display number entered in the first array
cout << endl << "You entered ";
for (int i = 0; i < count; i++)
{
cout << input[i] << " ";
}
cout << "\n";
//display how many times a number is entered.
for (int secondCount = 0; secondCount < MAX_NO; secondCount++)
{
cout << secondCount + 1 << " appears " << COUNT[secondCount]
<< " times in the input" << endl;
}
cout << endl;
system("pause");
return 0;
}
OUTPUT:
Please enter a number for value 1 = 1
Please enter a number for value 2 = 1
Please enter a number for value 3 = 1
Please enter a number for value 4 = 2
Please enter a number for value 5 = 3
Please enter a number for value 6 = 2
Please enter a number for value 7 = 4
Please enter a number for value 8 = 4
Please enter a number for value 9 = 3
Please enter a number for value 10 = 2
You entered: 1 1 1 2 3 2 4 4 3 2
1 appears 3 times in the input
2 appears 3 times in the input
3 appears 2 times in the input
4 appears 2 times in the input
5 appears 0 times in the input
for (int i = 1; i <= 5; i++)
{
if (input[i] == i)
{
COUNT[i]++;
}
}
Let's examine what this is doing. It starts by checking input[1]. (This should be input[0] as array indices start at 0). Then it checks if input[1] is equal to 1. If it is, then it increments COUNT[1].
Next, it checks input[2]. Then it checks if input[2] is equal to 2. If it is, it increments COUNT[2]. And so on until it has gone through input[5].
Do you see the problem with this? You're only checking the first 5 inputs and only checking them against a single value.

How to count how many times each number has been encountered?

I am trying to write a program to count each number the program has encountered. by putting M as an input for the number of the array elements and Max is for the maximum amount of number like you shouldn't exceed this number when writing an input in the M[i]. for some reason the program works just fine when I enter a small input like
Data input:
10 3
1 2 3 2 3 1 1 1 1 3
Answer:
5 2 3
But when I put a big input like 364 for array elements and 15 for example for max. the output doesn't work as expected and I can't find a reason for that!
#include "stdafx.h"
#include <iostream>
#include<fstream>
#include<string>
#include <stdio.h>
#include<conio.h>
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int checker[1000];
int element_cntr = 0;
int cntr = 0;
int n = 0;
void main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue>> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
checker[i]= M[i] ;
element_cntr++;
if (M[i] > Max)
{
cout << "the element number " << element_cntr << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (M[n] == checker[j])
{
cntr+=1;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
n++;
}
}
You have general algorithm problem and several code issues which make code hardly maintainable, non-readable and confusing. That's why you don't understand why it is not working.
Let's review it step by step.
The actual reason of incorrect output is that you only iterate through the first Max items of array when you need to iterate through the first Max integers. For example, let we have the input:
7 3
1 1 1 1 1 2 3
While the correct answer is: 5 1 1, your program will output 5 5 5, because in output loop it will iterate through the first three items and make output for them:
for (int i = 0; i < Max; i++)
for (int j = 0; j < ArrayValue; j++)
if (M[n] == checker[j]) // M[0] is 1, M[1] is 1 and M[2] is 1
It will output answers for first three items of initial array. In your example, it worked fine because the first three items were 1 2 3.
In order to make it work, you need to change your condition to
if (n == checker[j]) // oh, why do you need variable "n"? you have an "i" loop!
{
cntr += 1;
}
It will work, but both your code and algorithm are absolutely incorrect...
Not that proper solution
You have an unnecessary variable element_cntr - loop variable i will provide the same values. You are duplicating it's value.
Also, in your output loop you create a variable n while you have a loop variable i which does the same. You can safely remove variable n and replace if (M[n] == checker[j]) to if (M[i] == checker[j]).
Moreover, your checker array is a full copy if variable M. Why do you like to duplicate all the values? :)
Your code should look, at least, like this:
using namespace std;
int ArrayValue;
int Max;
int M[1000];
int cntr = 0;
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> ArrayValue >> Max;
for (int i = 0; i < ArrayValue; i++)
{
cin >> M[i];
if (M[i] > Max)
{
cout << "the element number " << i << " is bigger than " << Max << endl;
}
}
for (int i = 0; i < Max; i++)
{
cntr = 0;
for (int j = 0; j < ArrayValue; j++)
{
if (i == M[j])
{
cntr ++;
}
}
if (cntr != 0)
{
cout << cntr << " ";
}
}
return 0;
}
Proper solution
Why do you need a nested loop at all? You take O(n*m) operations to count the occurences of items. It can be easily counted with O(n) operations.
Just count them while reading:
using namespace std;
int arraySize;
int maxValue;
int counts[1000];
int main()
{
cout << "Enter the lenght of the Elements, followed by the maximum number: " << endl;
cin >> arraySize >> maxValue;
int lastReadValue;
for (int i = 0; i < arraySize; i++)
{
cin >> lastReadValue;
if (lastReadValue > maxValue)
cout << "Number " << i << " is bigger than maxValue! Skipping it..." << endl;
else
counts[lastReadValue]++; // read and increase the occurence count
}
for (int i = 0; i <= maxValue; i++)
{
if (counts[i] > 0)
cout << i << " occurences: " << counts[i] << endl; // output existent numbers
}
return 0;
}