Assignment: Function for storing sequence in array

Hello!
(Using windows, codeblocks, c++)
I'm trying to make a program to do following!
Make functions for:

//1. Ask user for sequence lenght: 0 <= k <= 300
Validate input

//2. Ask user to enter a sequence of integers
Store the integers in an array

//3. Display sequence by the same order they are entered and max 6 per line

I cannot use any loops in the main.



Can anybody help? :)


Last edited on
use loop outside main?
Yes. But i cannot make it work..
Have not used arrays outside main before.
1
2
3
4
5
6
7
8
9
10
11
void doSomething(int *ptr)
{    
	for(int i=0; i<100; i++) 
		cout << ptr[i] << " ";        
}   
 
int main()
{	int ar[100] ={0};
    doSomething (ar);
    return 0;
}
closed account (28poGNh0)
Maybe you're looking for this

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# include <iostream>
using namespace std;

int getSequenceLength(void);
void storeIntergerInAnArrayFunction(int);

int main()
{
    storeIntergerInAnArrayFunction(getSequenceLength());
}

int getSequenceLength(void)
{
    int seqLength;

    while(true)
    {
        cout << "Enter the sequence length -> ";cin >> seqLength;

        if(seqLength>=0&&seqLength<=300)
            break;
    }return seqLength;
}

void storeIntergerInAnArrayFunction(int nbr)
{
    int anArray[nbr];

    cout << "Please enter the sequence of integers" << endl;
    for(int i=0;i<nbr;i++)
    {
        cout << "Interger nbr " << i+1 << " -> ";
        cin >> anArray[i];
    }

    int iMax = 0,iCount = 0;

    for(int i=0;i<nbr;i++)
    {
        cout << anArray[i] << " ";
        iMax = max(iMax,anArray[i]);
        iCount++;

        if(iCount == 6)
        {
            cout << "The max is : " << iMax << endl;
            iMax = iCount = 0;
        }
    }
}


Good look
what does *ptr mean?
YES! Something like that!! :) :) thank you!!
i'm not sure how i call the function in the main?
int getSequenceLength(void);
void storeIntergerInAnArrayFunction(int);

Are these global variables? Or what do these say??
closed account (28poGNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
type function(type,..) // function prototype 


int main()
{
     function(someVariable,..); // if you write what you named the funtion in the main function 
                      // we say that you called the function 
}

type function(type,...) // function body or implementation
{
     // some code
}

hope that helped
Last edited on
closed account (28poGNh0)
int getSequenceLength(void);
void storeIntergerInAnArrayFunction(int);

Are these global variables? Or what do these say??


those are functions check above
Trying to do something like this, but now the main does not work!!

# include <iostream>
using namespace std;

int Sequence_Length(int seq_length)
{
do{
cout << "Enter the sequence length: ";
cin >> seq_length;

if(seq_length >= 0 && seq_length <= 300)
break;
}
while (seq_length >= 0 && seq_length <= 300);

return seq_length;
}

void storesequence(int seq[], int n)
{

cout << "Please enter the sequence of integers: " << endl;
for(int i=0;i<n;i++)
{
cin >> seq[i];
}

for(int i=0;i<n;i++)
{
cout << seq[i] << " ";
}
}


int main()
{
storesequence(Sequence_Length());

return 0;
}
[/output]
closed account (28poGNh0)
read some turorials first instead of gambling http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
Topic archived. No new replies allowed.