1
#include <iostream>
using namespace std;
int main()
{
    int x, y, z;
    cout << "how many elements? " << endl;
    cin >> x;
    int arr[x];
    for(int i = 0; i < x; i++)
    {
        cout << "give number" << endl;
        cin >> arr[i];
    }
    cout << endl;
    y = 0;
    z = 0;
//  for(int i = y; i >= 0; i-z)
    while (z < x)
    {
        for(int i = y; i < x; i++)
        {
            for(int i = z; i < x-y; i++)
            {
                cout << arr[i] << " ";
            }
            cout << endl;
            y++;
        }
        z++;
        cout << z;
    }
//  while (z < x);
    return 0;
}

I want my parameter z, which is in the while loop, to link with parameter i=z in the second for loop. I want the program to print numbers from 1 to n, then from 2 to n, and so on, to the moment it will print only n.

Don't mind this cout<<z; in line 30, I was checking if this even works.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Blaze
  • 11
  • 1
  • 5
    Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – cigien Nov 01 '21 at 19:18
  • 1
    For starters, use different variable names for the loop variables. e.g.: use `i` in the outer loop and `j` in the inner loop. Then you can refer to `i` in crafting your inner loop. – Wyck Nov 01 '21 at 19:21
  • 2
    `int arr[x];` is [not standard C++](https://stackoverflow.com/questions/1887097/) when `x` is not a compile-time constant. Use `std::vector` instead when you don't know the array size at compile-time. – Remy Lebeau Nov 01 '21 at 19:23

2 Answers2

2

I don't know what you mean by "link with z", but here is what the loop should be to do what you want it to:

for (int z = 0; z < x; ++z) // NOTE: get rid of outside z
    {
        for(int i{z}; i < x; ++i)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
    }

The inner loop loops through all of the values of the array, starting with z and ending at x-1. Since z is incremented, the next loop will start at the next position, starting at what was z plus one and ending at x-1.

Also change:

int arr[x];

To:

int* arr = new int[x];

and add this line at the end:

delete [] arr;

This should be added and changed because C++ doesn't support variable length arrays. However, it does support pointers to blocks of memory treated as arrays. You also should delete memory you use when you are done with it.

Captain Hatteras
  • 490
  • 3
  • 11
2

In c++, while declaring an array either the size or the elements must be known at the time of compilation, if you want to declare an array during run time, you can do one of the following

  • you can use std::vector, which is a dynamically growing array, and can be initialized without the size or elements, it is included in the vector header file.
  • use the new keyword to dynamically allocate a block of memory on the heap which returns a pointer and will act as your array (refer to @captainhatteras 's answer)

However it is better to use vector in this case as it is much more easier to understand and use

And you don't need the nested loop here, (not sure if you were keeping it for future reference, i removed it for the sake of simplicity)

How it works

Here with each iteration of the while loop, we're incrementing the value of int z which is taken as the starting point of our for-loop, therefore with each passing iteration the range of the for-loop is decreased by 1. ultimately the value of int z will be equal to int x and the code will exit the while loop

so your code would look something like this

#include <iostream>
#include <vector>
using namespace std;
int main() {
    int x, z = 0;
    cout << "how many elements? " << endl;
    cin >> x;
    vector<int> arr;
    for (int i = 0; i < x; i++) {
        int choice;
        cout << "give number" << endl;
        cin >> choice;
        arr.push_back(choice);
    }
    cout << endl;
    while (z < x) {
        for (int i = z; i < x; i++)
            std::cout << arr[i] << " ";
        std::cout << std::endl;
        z++;
    }
    return 0;
}

std::vector has a method called push_back() which adds an element passed in to the end of the array

AnanthDev
  • 1,605
  • 1
  • 4
  • 14
  • Since Blaze already knows the size of the array from the input x, vector here should not be used, as push_back can cause reallocation. – Captain Hatteras Nov 01 '21 at 19:42
  • yea ill edit my answer one sec – AnanthDev Nov 01 '21 at 19:45
  • 2
    @CaptainHatteras It's fine to use a vector in such a case and just use `reserve` if you're concerned about reallocations. It's certainly preferable to manual memory management. – Kyle Nov 01 '21 at 19:45
  • It's probably clearer to express that while loop as a for loop `for(int z=0; z – SirGuy Nov 01 '21 at 19:48
  • @Kyle so keep this answer? or add dynamic allocation – AnanthDev Nov 01 '21 at 19:48
  • @AnanthDev I'd keep it as is. Maybe add a `reserve` call if you're so inclined with a comment about what it does, but I wouldn't worry too much about it. There's many more pitfalls with a direct dynamic allocation than just letting `vector` take care of everything. – Kyle Nov 01 '21 at 19:51
  • ah alright thanks for the feedback :) – AnanthDev Nov 01 '21 at 19:52