Nested "for" loops c++ - c++

On the way to understand working of nested for loop i wrote a program that takes a input and display a pyramid upto that input value like this:
1
22
333
4444
It's displaying just the height of pyramid but its not displaying written part in the second for loop.
Here is the code(after modification but required result not yet)
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max;
for (int j = 0 ; j <= max ; j++)
{
cout << j ;
}
cout << endl ;
max++ ;
}
system("PAUSE");
return 0;
}

#include <iostream>
using namespace std;
int main(void)
{
int num ;
cout << "Enter the number of pyramid" << endl ;
cin >> num ;
for (int i = 0; i < num ; i++)
{
int max = i +1; //change 1
for (int j = 0 ; j < max ; j++)
{
cout << max; //change 2
}
cout << endl ;
//max++ ; //change 3
}
system("PAUSE") ;
return 0;
}

You should initialize max to 0.
int max = 0;
Additionally there are two more bugs.
int max ;
should be declared before the for loop for i. (Otherwise max is defined to be 0 always)
In the inner loop print i, not j.

First of all, please try to have a proper structure in your code:
#include <iostream>
using namespace std;
int main(void)
{
int num;
cout << "Enter the number of pyramid" << endl;
cin >> num;
for(int i = 0; i < num; i++)
{
int max;
for(int j = 0; j <= max; j++)
{
cout << j;
}
cout << endl;
max++;
}
system("PAUSE");
return 0;
}
And your mistake:
Change int max; to int max = 0;
You cannot add 1 to a non existing value.

As has been stated in other answers, your max counter isn't initialized. Additionally, you don't really need it, as you already have i doing the same task:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
{
cout << i;
}
cout << endl;
}

Unless you actually want to print something like 0 01 012 0123, this is the code you're looking for:
for (int i = 1; i <= num; i++)
{
for (int j = 0; j < i; j++)
cout << i;
cout << endl;
}

max is not set to an initial value.
Its declared insides 1st loop and then used in the 2nd loop.

Related

How do i flip a number half triangle in c++?

I need to code this in c++ using nested for loops. click thisAnd this is what I have right now.
#include <iostream>
#include <map>
using namespace std;
int main() {
int i, j, k, n = 9;
for(i = n; i >= 0; i--){
for(j = i; j >= 0; j--)
cout << j<<" ";
cout <<endl;
}
}
how do I flip it.
Currently you are starting with the longest row and going down. Start with the shortest row and go up:
#include <iostream>
int main() {
int n = 9;
for(int i = 0; i <= n; i++){ // start with 0 and go up to n
for(int j = i; j >= 0; j--)
std::cout << j << ' ';
std::cout << '\n';
}
}
To flip in the other direction fill it with spaces:
#include <iostream>
int main() {
int n = 9;
for(int i = n; i >= 0; i--){
for(int j = 2*(n-i); j >= 0; j--) // fill with spaces
std::cout << ' ';
for(int j = 0; j <= i; j++) // start with 0 and go up to i
std::cout << j << ' ';
std::cout << '\n';
}
}
#include <iostream>
using namespace std;
int main()
{
for (int i = 9; i >= 0; i--) { // i is 9 and its decrementing over time
for (int j = 9; j > i; j--) { // j is also 9 and it must be greater than i, otherwise you will have unnecessary one blank space in first row
cout << " "; // This is my way of doing things, i would rather print double blank space
}
for (int k = 0; k <= i; k++) { // k is just incrementing from 0 to i and it has blank space
cout << k << " ";
}
cout << endl;
}
}

How to fill a 2D through input and then read back as output in a C++. Here is the code which I wrote. I gives wrong output.I am a beginner

#include <iostream>
#include<fstream>
using namespace std;
int main() {
int a = 1;
if (a == 1) {
int a[1][1];
for (int i = 0; i <= 1; i++) {
for (int j = 0; j <= 1; j++) {
cin >> a[i][j];
}
}
cout << endl;
for (int k = 0; k <= 1; k++) {
for (int l = 0; l <= 1; l++) {
cout << a[k][l] << " ";
}
cout << endl;
}
}
return 0;
}
In this program if we enter input as :
1
2
3
4
it gives output :
1 3
3 1
it should give output as:
1 2
3 4
Please help, I am a beginner.
I am coding in code blocks.
Try this
int main()
{
int arr[2][2]; // declares a 2x2 array
cout << "Enter integers : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cin >> arr[i][j]; //inserts at the index i and j in the array
}
}
cout << "Display array : " << endl;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << arr[i][j] << endl; /*displays the value at
index i and j in the array*/
}
}
}

How do you have a user input values in an array using a function in C++?

Ok so I have this code to have users input numbers into an array but would like to make it into a function. I am new to C++ and am unsure how to go about it. Can anyone give me some hints or tips? I would greatly appreciate it. The code is as follows:
main()
{
int m;
int n;
int i;
int j;
int A[100][100];
m = get_input ();
n = get_input2 ();
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (i = 0 ; i < m ; i++)
for (j = 0 ; j < n ; j++)
cin >> A[i][j];
return;
}
void initialize(int x[][], int m, int n){
cout << endl << "Enter positive integers for the elements of matrix A:" << endl;
for (int i = 0 ; i < m ; i++)
for (int j = 0 ; j < n ; j++)
cin >> x[i][j];
}
main()
{
int A[100][100],m,n;
// m = get_input ();// you must have that method or cin>>m
//n = get_input2 (); //you must have that method or cin>>n
cout<<"Enter no of rows";
cin>>m;
cout<<"Enter no of columns";
cin>>n;
initialize (A,m,n);
return;
}

Incorrect Result from Selection Sort Algorithm

#include <iostream>
using namespace std;
// Selection Sort function.
// Parameter 'a' is the size of the array.
void ss(int AR[] , int a) {
int small;
for (int i = 0 ; i <a ; i++) {
small = AR[i];
for (int j = i+1 ; j <a ; j++) {
if (AR[j]< small) {
int k = AR[j];
AR[j] = AR[i];
AR[i] = k;
}
}
}
}
int main() {
cout << "Enter the size of Your Aray";
int a;
cin >> a;
int AR[a];
cout << endl;
for (int i = 0; i < a; i++) {
cin >> AR[i];
cout << endl;
}
ss(AR, a);
cout << "The Sorted Array is";
for (int i=0; i < a; i++) {
cout << AR[i] << " ";
cout << endl;
}
}
When I enter the following:
15
6
13
22
23
52
2
The result returned is:
2
13
6
15
22
23
52
What is the bug preventing the list from being sorted numerically as expected?
The function can look like
void ss ( int a[], size_t n )
{
for ( size_t i = 0 ; i < n ; i++ )
{
size _t small = i;
for ( size_t j = i + 1; j < n ; j++ )
{
if ( a[j] < a[small] ) small = j;
}
if ( i != small )
{
int tmp = a[small];
a[small] = a[i];
a[i] = tmp;
}
}
}
It doesn't seem to be the SelectionSort I know. in the algorithm I know during every loop I look for the smallest element in the right subarray and than exchange it with the "pivot" element of the loop. Here's the algorithm
void selectionSort(int* a, int dim)
{
int posMin , aux;
for(int i = 0; i < dim - 1; ++i)
{
posMin = i;
for(int j = i + 1; j < dim; ++j)
{
if(a[j] < a[posMin])
posMin = j;
}
aux = a[i];
a[i] = a[posMin];
a[posMin] = aux;
}
}
and it seems that you change every smaller element you find, but also change the position of the "pivot". I hope the answer is clear.
Everything is ok in the original function, only that the small variable need to be refreshed when two vector elements will be switched.
Also in if statement set the small variable to the new value of AR[i].

print stars as much as the values in the array

I'm trying to make a C++ program start creating an array and takes the values from the user , then print every value + star as much the value is .. Example : the user had entered 5 then the output must be like this
5*****
Input
1
2
3
4
5
6
output
1*
2**
3***
4****
and so on
.. help :(
#include <iostream>
using namespace std;
void main()
{
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; x <= arr[i]; j++)
{
cout<< "*";
}
}
}
And another help please can you give me some useful link to practice on programming to be professional
Your code is wrong. Use the following code:
#include <iostream>
using namespace std;
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
int x = arr[i];
for (int j = 0; j < x; j++){ // your condition was wrong
cout<< "*";
}
cout<<endl; // for better formatting
}
return 0;
}
For edited question
int main() {
int arr[10];
for (int i = 0; i < 10; i++)
{
cin >> arr[i];
}
for (int i = 0; i < 10; i++)
{
int x = arr[i];
cout << x;
for (int j = 0; j < x; j++){ // your condition was wrong
cout << "*";
}
cout << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void main()
{
int nbValues = 10;
int arr[nbValues];
// First recover the values
for (int i = 0; i < nbValues; i++)
{
cin >> arr[i];
}
// Then print the output
for (int i = 0; i < nbValues; i++)
{
int x = arr[i];
cout << x;// Print the number
for (int j = 0; j < x; j++)
{
cout<< "*";// Then print the stars
}
cout << endl;// Then new line
}
}