Learning Arrays

I need help with the loop of my array program. The point of the program is to ask a user for a certain number of integers and then read that number of integers into arrays. the code I have compiles but it just prints out the copyright notice reads the number of integers and then exits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// --------------------------------------------------------------------
// File name:   oddArray.cpp
// Assign ID:   LAB12
// Due Date:    11/30/12 at 12:00 pm 
//
// Purpose:     Initializes an array that stores 7 integer variables.
//
// Author:      bfields Byron Fields
// --------------------------------------------------------------------

#include <iostream>

using namespace std;

int main()
{
int k;
int array[10];
int l;
cout << "(c) 2012, bfields Byron Fields" << endl;
cout << "Enter an integer number (to a max of 10) for the array" << endl;
cin >> l;

for (k=0; k == l; k++)
{
cin >> array[k];
cout << "Element " << k << " is " << array[k] << endl;

}



return 0;
}


OMG! First of all, PLEASE do not use the lowercase letter "L" as a variable. It looks too much like the number 1 and will confuse other programmers.

Second, change your for () statement to:
for (k=0; k < l; k++)

You also might want to add a prompt message before your cin statement in the for loop.
Last edited on
yeah I was just being lazy on that one. Thanks for the input. IT works now. But what kind of loop would I need to use for the program to print out the elements one after the other after all the integers have been entered.

i.e.

element 1 is:
element 2 is:
element 3 is:
for, while or do
up to you if you know how to use them
yeah I understand and know how to use these loops. I guess the only thing I can't quite figure out right now is how do I code:

cout << array[k];

and make sure I get each different value for k.
nevermind I had an aha! moment soon as I typed that .
okay last problem. How could I could this program to tell whether or not the integers entered are in ascending or descending order .

I tried

if(array[k] > array[k++])
{
cout << "array is in descending order" << endl;
}
you only check 2 elements there, not the whole array
also you dont check for k < k+1

also change k++ to k+1. it doesnt work as index
Last edited on
Topic archived. No new replies allowed.