-2
#include <iostream>

using namespace std;

int main() {
  int marks[1000], i, j;
  cout << "Enter the size of an array: ";
  cin >> i;
  for (j = 0; j <= i; j++) {
    cout << "Enter " << i << " element of array: ";
    cin >> marks[i];
    i++;
  }
  for (j = 0; j <= i; j++) {
    cout << "The " << i << " element of array is: " << endl;
    i++;
  }
  return 0;
}

I declared an integer array initially along with i and j variables. Then I asked the user for the size of an array and then assigned it to i variable. Then i initialized a for loop and asked the user to input the element of an array and stored them in i of array. I think I made some mistake in for loop so can you guys help me with that?

Biffen
  • 6,249
  • 6
  • 28
  • 36
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. For example, [Printing an array in C++?](https://stackoverflow.com/questions/1370323/printing-an-array-in-c). – Jason Nov 15 '22 at 10:47

1 Answers1

0

You're missing the actual printing of the element. Also, in the loops, you don't need to increment the size of the arrays:

    for ( j = 0; j < i; j++)
    {
        cout<<"Enter "<<j<<" element of array: ";
        cin>>marks[j];
    }

    for ( j = 0; j < i; j++)
    {
        cout<<"The "<<j<<" element of array is: "<<marks[j]<<endl;
    }

It's advised to use more descriptive name for variables (e.g. elemCount) so that you can avoid these kind of mistakes.

lorro
  • 10,687
  • 23
  • 36
  • 1
    You should use `j` instead of `i` both for `cout` and `cin` in the first loop. Also, I think the loop conditions should use `<` instead of `<=`, but that can only be clarified by the OP. – Fabio says Reinstate Monica Nov 15 '22 at 10:51