inverted triangle using while loop c++ - c++

Cant get to where I need to be. So I need a program using while loops only to get an output of . .
123456
12345
1234
123
12
1
Given, you enter the number amount of rows and in this case it is 6. My program now displays this
1
12
123
1234
12345
123456
Any help on inverting this? Program currently appears as . .
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int rows;
int i;
int j;
cout << "Enter number of rows: ";
cin >> rows;
int k=rows;
i=1;
while (i <= rows)
{
j=1;
while(j <= i)
{
cout << j%10;
j++;
}
cout << endl ;
i++;
}
}

Since you want to have a decreasing number of outputs you have to start your outer loop with the given input and decrease it for every iteration.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int rows;
cout << "Enter number of rows: ";
cin >> rows;
int remainingRows = rows;
while (remainingRows >= 1)
{
int outputIndex = 1;
while(outputIndex <= remainingRows)
{
cout << outputIndex%10;
outputIndex++;
}
cout << endl ;
remainingRows--;
}
}
DEMO

Related

C++ Write a function to input a list of numbers and print the frequency of the first input number

I am trying to write a C++ program to ask user to input a list of 5 numbers and print out the counts of the first number in the input. I have been trying to use array[] but I have some problems. The ideal inputs and outputs are :
Input : 1 1 2 3 1 Output: 3 because there are 3 counts of 1
Input : 1 2 3 4 5 Output: 1
Input : 1 1 1 0 0 Output: 3
Here are my codes, my code allows me to take the inputs but it does not do anything with it. Any help is appreciated!
#include <iostream>
using namespace std;
//frequency function
int frequency(int a[])
{
int count = 0;
for (int i=0; i < 6; i++)
if (a[i] == a[0])
{
count++;
}
cout << count << endl ;
return count;
}
// Driver program
int main() {
int i;
cout << "Please enter your numbers: ";
int a[5] = {a[0],a[1],a[2],a[3],a[4]};
for (i = 1; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[0] >> a[1] >> a[2]>> a[3] >> a[4];
return 0;
}
int n = sizeof(a)/sizeof(a[0]);
cout << frequency(a);
}
I tried another simpler approach but it needs a little more help
#include <iostream>
#include <string>
using namespace std ;
int main(){
cout << "Please enter your numbers: ";
int a[5];
int repeat;
int first = a[0];
int i;
for (i = 0; i < 6; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
if (a[i] == first){
repeat++;
}
cout << "Count: " << repeat;
}
you have an odd mixture of techniques for reading a set of numbers. You simply need
cout << "Please enter your numbers: ";
int a[5];
for (int i = 0; i < 5; i++)
{
// Reading User Input value Based on index
cin >> a[i];
}
and you certainly dont want that return as it will end the program
you would be better off using std::vector then you would not need to hard code the array size.
Note at the moment you have 6 as the arrays size in 'frequency' but 5 in main

counting the number of values in an array with autofilled elements from rand function in c++

I am currently working on a program that generates random numbers within a given range and that is saved into an array in which I can count the number of the value in the array that is desired. The program that I made works fine without altering compilation error, however, I always get a 0 for the result of the count function, no matter the existence of the element that I'm counting for. Is there a way to fix this problem?
This is the source code of random_function.cpp
#include <iostream>
#include "random_function.hpp"
using namespace std;
Rand::Rand()
{
}
void Rand::Random_function()
{
cout << "Enter amount of numbers to generate: ";
cin >> range;
if (range<=0)
{
cout <<"Error! Please enter the valid number and try again! "<<endl;
}
else {
cout << "Enter minimum boundary: ";
cin >> min;
cout << "Enter maximum boundary: ";
cin >> max;
cout << "\n";
if(max<=min || cin.fail())
{
cout << "\nError! Please enter the valid value and try again! " << endl;
}
else {
srand((unsigned)time(NULL));
for(int n=1 ; n <= range; n++)
{
cout << min + (rand() % static_cast<int>(max - min + 1)) <<endl;
}
}
cout <<"\n"<< "Total random numbers generated: " << range<< endl;
}
}
void Rand::Count_function()
{
extern int y;
int array[range];
int count=0;
int random= min+rand() % static_cast<int>(max - min + 1);
//for some reason this for loop is not working. But there isn't any compilation error.
for (int i=0; i<range; i++)
{
array[i] = random;
if(array[i]==y)
count++;
}
//This part works fine, its just that for loop above doesn't
cout <<"The number of '"<<y<<"'s in the given list is: "<< count <<endl;
}
This is a source code of the random function.hpp
#pragma once
using namespace std;
class Rand
{
public:
Rand();
int range,min,max;
void Random_function();
void Count_function();
};
This is main.cpp
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <random>
#include "random_function.hpp"
using namespace std;
int y;
int main()
{
Rand call;
call.Random_function();
cout<<"Enter the value to count for: ";
cin>>y;
Rand call2;
call2.Count_function();
}

c++ shorter way to display the next 5 numbers by given inputs?

i've just learned cpp and want to know is there a shorter way to display a sequential number.
this is my code
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
A simple loop should solve your problem:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}

C++ simple matrix

can someone fix my code?
this is the result that should be showed when i input number 5 in c++
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
my result:
1
2 6
3 7 10
4 8 11 14
5 9 12 15 18
my code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
int n,i,j;
cout<<"insert number"<<endl;
cin>>n;
for (i=1;i<=n;i++)
{
int y=1;
int g=1;
cout<<i<<" ";
for (j=1;j<=i-1;j++)
{
int x=n;
int b=i;
x--;
g--;
cout<<(x*y)+b+g<<" ";
y++;
}
cout<<endl;
}
getch ();
}
what did i do wrong?
sorry if my code messy i'm a c++ new learner.
You could it like this:
#include <iostream>
using namespace std;
int main()
{
int n , i ,j, sum;
cout << "masukkan bilanga" << endl;
cin >> n;
for(i = 0; i < n; i++)
{
cout << i + 1 << " ";
sum = i + 1;
for(j = 0; j < i; j++)
{
sum += n - 1 - j;
cout << sum << " ";
}
cout << endl;
}
return 0;
}
where the key point is that you want to print all the numbers, starting from 1, in a column-major manner, until a triangular n x n matrix is created.
Driven from the output, one can easily see that every element of the next column is what the current element is, plus n - 1, and that factor decreases by one as we advance to the right part of the matrix.
Try this code, hope this is what you need:
#include <iostream>
#include <string>
void printMatrix(int number);
void main()
{
int number = 1;
std::cout << "Enter a number: ";
std::cin >> number;
printMatrix(number);
}
void printMatrix(int number) {
std::cout << std::endl;
for (int i = 1; i <= number; i++) {
std::cout << i << " ";
int n = i;
for(int j = 1; j < i; j++) {
n += number - j;
std::cout << n << " ";
}
std::cout << std::endl;
}
}

How can I find a missing number in a sequence of numbers?

Numbers should be input as:
53,55,57,58,54
Output:
Missing number is 56
Input should be able to be as many numbers as wanted.
This is what I have so far:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int getMissingNo (int a[], int n)
{
int i, total;
total = (n+1)*(n+2)/2;
for ( i = 0; i< n; i++)
total -= a[i];
return total;
}
int main()
{
int a[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter number of numbers in sequence: ";
int numInSeq;
cin >> numInSeq;
cout << endl;
cout << "Enter numbers in sequence: ";
for (int i = 0; i < numInSeq; i++)
{
cin >> a[i];
}
cout << endl;
int miss = getMissingNo(a,numInSeq);
cout << "Missing number: " << miss << endl << endl;
return 0;
}
The only thing I am missing is being able to enter the numbers separated by commas and I need to edit getMissingNo so it can be any sequence of numbers not just one that starts with 1.
Simple solution if you know the range.
For a given range, let S be the sum of all the numbers within that range.
For a given array with a missing number, let MS be the sum of the numbers in this array.
The missing number is S - MS