Home > Software design >  Can I somehow link parameter inside loop which is inside loop with parameter inside first loop?
Can I somehow link parameter inside loop which is inside loop with parameter inside first loop?

Time:11-02

#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.

CodePudding user response:

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.

CodePudding user response:

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

  •  Tags:  
  • c
  • Related