struggling now!!

Pages: 12
ok i am suppose to write a nested loop which prompt the user to input how many list and how many number in each list that u entered and also their average. but i read and read and test i couldnt find the solution. care to help? example:

how many list? = 2

list 1 = how many number = 3
x = 1
x = 2
x = 3

the average of list 1 is 2

list 2 = how many number = 5
x = 1
x = 2
x = 3
x = 4
x = 5

the average of list 2 is 3


i think we are suppose to input the x value??
Not gonna do your homework for you, buut I'll help you out.


Use a for loop. do you know the syntax for that??
Last edited on
yeah im using for loop but looping here looping there i dont seem to loop to the place that i want
k, post the code you have.
i dont think im at anywhere yet >.<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
    int i , a , count;

cout << "Enter List: " ;
cin >> a ;
for(i = 0; i <= a; i++)
{   
    cout << "List " << endl;  
}   

      
      
      
      system("pause");
      return 0;
}
heh kk...If you want to input a list of variables, and deal with them in a loop you'll need an array. At the risk of looking presumptuous...
int list1[255]; creates a list with 256 elements, counting element 0
or you could dymically allocate them with a pointer.
int *list1 = new int[a];

where a = the user defined length of the list...just remember, you MUST free this allocation before you move one with the code, if not you're looking at possibly your first memory leak, and those are bad xD

delete is implemented as such

delete list1;



array huh? new to me.. never heard of it and lecturer never mention whats an array is.. did he expect us to do it? or could it be done without using this array method?
Not easily. He probably mentioned in passing at the beginning of the semester, or some Professor mentioned it early in your programming education in university. Do you have a text book? if so go to the index and look up arrays. and pointers.
ok i saw array chapter but it is in chapter 9 while my homework is at chapter 6.. i didnt even learn it and so i got to do a homework with chapter 9 function in it.. OMG
In the infamous words of Ford Prefect 'Don't Panic.' Arrays aren't that hard to work with...think of an array as a a numbered list of items. with the list starting at 0.

so if you want an array of integers (int) with 7 elements total (including 0) declare it like this.
int array[6];

just remember that doing that uses the same amount of RAM as it would to declare 6 separate integers.

edit:

To work this into your loop you would do something like this
1
2
3
4
5
6
int count[a];
for(i=0; i<=a;i++){
         cout << "List value" << i <<  "= ";
         cin >> count[i]; 
         cout  << endl;
}

Last edited on
int list1[255]; creates a list with 256 elements, counting element 0
wrong!

It does not hurt to rough out the algorithm with a pencil and paper
Maybe the first question should be: "How many lists to be made?"
cin>>a will then get the key board response (say 5)
Now you want the operator to give you the list so:
cout<<"enter the numbers in your first list (separated by spaces)
getline(cin,s)//you need to have previously declared string s.
You now have list #1 in s.
Now in the body of a for loop with finish condition of 1<5 use getline(cin,s[i]) to get s1,s2,s3 and s4. after a suitable prompt. Note:You can obtain the number of integers in each list by calling s.length(). You can print out each string with cout<<s;
Now you have 5 strings each one with a list of integers in it.
So process each string in a new for loop to obtain average from sum /s.length
This may not be the most elegant solution but it is not complicated
Last edited on
._. How is that wrong? care to explain to me? Not trying to pick a fight just curious, as that's what i've ALWAYS heard...

edit: major derp on my end...Sorry it's late as usual when I'm on these forums...yea, buffbill is right. declaring int list1[255] would give you 255 elements...with 0 being the start and 254 being the end :P
Last edited on
ok the list is good i think i can use it.. and if i want to end it i'll just break it out right?
end the loop? or end the array?
loop
oh yea, you can just break from it if you need to. But if it's a for loop, it'll end automatically once the middle condition is true....ie for(int i = 1, i<=12, i++); will run 12 times before ending.
i think i change my format... like the list.. starting with list 1 then enter list 1 number then continue with list 2 then enter their number.. max i put 1000 so if they get bored of it just break it... cool?
Sounds good :D
im still having problem to input number in the list >.< dont know how to write it..
now my code is

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
#include <iostream>
using namespace std;

int main()
{
    int  a , b , i , total ;
    total = 0;
     
     
for(i=1; i<=a;i++)
{
    cout << "List value = ";
    cin >> a;
    
    for(i=1;i<=b;i++)
    {
    cout << "List " << i << " numbers: " ;
    cin >> b;
    cout << "The number is " << endl;
    }

         
}  

      system("pause");
      return 0;
}
Last edited on
ok i solved my problem thanks though eventhough i not quite understand what u all teaching me xD
Pages: 12