please HELP with my class list
Mar 7, 2012 at 2:53pm UTC
hello; so i have this assignment
Write a program that define a class IntegerList with the following member variables:
Int counter
Int list[100]
Member functions: Write the implementation outside the class
Default Constructor (IntegerList): will give zero to the counter and to the array
Void insert(int a): This function will insert a number (a) to the array list and increase the counter
Bool Find(int a): This function return true if (a) in the array otherwise it will return false
Bool isEmpty(): This function will return true if the array is empty otherwise it will return false
Bool isFull(): This function will return true if the array is full
IntegerList Add(IntegerList ): this function will return an object of class IntegerList after make a summation between two objects (The calling object and the parameter object)
Void increment(): this function will increase the values of the array by one
Void print(): this function will print the array.
In the main function
Deaclare two objects obj1, obj2 from class IntegerList
Insert values to both list
Call isFull(), isEmpty(), and Find(int a) function
Call increment()
Declare a third object obj3 and
Obj3= obj1.Add(obj2)
Then call print () function
but english isn't my first language so i'm not sure i fully understood the question!!!!!!
but i wrote this code and i hope someone would help me and tell me if i got it right
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#include <iostream>
using namespace std;
class integerList
{
int counter, list[100];
public :
integerList ();
void insert(int a,int i);
bool Find(int a);
bool isEmpty();
bool isFull();
integerList Add(integerList &);
void increment();
void print();
};
void main()
{
integerList obj1,obj2,obj3;
obj1.insert(100,0);//put 100 in list[0]
obj2.insert(200,2);//put 200 in list [2]
cout<<"the first array is: " ;
if (obj1.isEmpty() ==false )
cout<<"not empty" <<endl;
else
cout<<"empty" <<endl;
cout<<"and: " ;
if (obj1.isFull()==true )
cout<<"full" <<endl;
else
cout<<"not full" <<endl;
cout<<"the second array is: " ;
if (obj2.isEmpty() ==false )
cout<<"not empty" <<endl;
else
cout<<"empty" <<endl;
cout<<"and: " ;
if (obj2.isFull()==true )
cout<<"full" <<endl;
else
cout<<"not full" <<endl;
if (obj1.Find(2)==true )
cout<<"2 exist in the first array" ;
else
cout<<"2 does not exist in the first array" <<endl;
obj1.increment();
obj2.increment();
obj3= obj2.Add(obj1);
cout<<"the first array is : " ;
obj1.print();
cout<<endl;
cout<<"the second array is : " ;
obj2.print();
cout<<endl;
cout<<"the third array is : " ;
obj3.print();
cout<<endl;
}
integerList::integerList()
{
counter=0;
for (int i=0;i<100;i++)
list[i]=0;
}
void integerList::insert(int a,int i)//i:to ddefine where to put the number(a) in the array
{
list[i]=a;
counter++;
}
bool integerList::Find(int a)
{
bool exists;
for (int i=0;i<100;i++)
if (list[i]==a)
{
exists = true ;
break ;
}
else
exists= false ;
return exists;
}
bool integerList::isEmpty()
{
bool empty;
for (int i=0;i<100;i++)
if (list[i]!=0)
{
empty= false ;
break ;
}
else
empty = true ;
return empty;
}
bool integerList::isFull()
{
bool full;
for (int i=0;i<100;i++)
if (list[i]==0)
{
full=false ;
break ;
}
else
full= true ;
return full;
}
integerList integerList::Add(integerList & object)
{
integerList temp;
for (int i=0;i<100;i++)
temp.list[i]=list[i]+ object.list[i];
return object;
}
void integerList::increment()
{
for (int i=0;i<100;i++)
list[i]++;
}
void integerList::print()
{
for (int i=0;i<100;i++)
cout<<list[i]<<" " ;
cout<<endl;
}
also please tell me why the
integerList Add(integerList &); isn't functioning the right way
thank you so much ^_^
Mar 7, 2012 at 3:11pm UTC
You are directed to write a function
void insert(int a)
but you have written
void insert(int a,int i)
Mar 7, 2012 at 4:13pm UTC
You are directed to write a function
void insert(int a)
but you have written
void insert(int a,int i)
but that doesn't make any sense !!!!
where is the a going to be in the array?!?!?!
Mar 7, 2012 at 4:25pm UTC
How about at the end? Or the beginning? Or how about you go back and double-check the question? I assure you that when you do not understand the task you have been given, pretending it is actually a different task and doing that task instead is not a good idea.
Mar 7, 2012 at 4:28pm UTC
to
i just said that english is'nt my first language so this IS what i understood!!!
am not pretending its somethin else!
Mar 7, 2012 at 4:37pm UTC
am not pretending its somethin else!
You presented this as your task:
Void insert(int a): This function will insert a number (a) to the array list and increase the counter
and then you actually created this:
void insert(int a,int i);
which does NOT match the task you presented.
Don't shout at me for pointing out that your task does not match what you have done.
Mar 7, 2012 at 5:06pm UTC
am sorry i didnt mean to sound like am shouting
anyway i corrected the code to 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#include <iostream>
using namespace std;
class integerList
{
int counter;
int list[100];
public :
integerList ();
void insert(int a);
bool Find(int a);
bool isEmpty();
bool isFull();
integerList Add(integerList &);
void increment();
void print();
};
void main()
{
integerList obj1,obj2,obj3;
obj1.insert(100);
obj2.insert(200);
cout<<"the first array is: " ;
if (obj1.isEmpty() ==false )
cout<<"not empty" <<endl;
else
cout<<"empty" <<endl;
cout<<"and: " ;
if (obj1.isFull()==true )
cout<<"full" <<endl;
else
cout<<"not full" <<endl;
cout<<"the second array is: " ;
if (obj2.isEmpty() ==false )
cout<<"not empty" <<endl;
else
cout<<"empty" <<endl;
cout<<"and: " ;
if (obj2.isFull()==true )
cout<<"full" <<endl;
else
cout<<"not full" <<endl;
if (obj1.Find(2)==true )
cout<<"2 exist in the first array" ;
else
cout<<"2 does not exist in the first array" <<endl;
obj1.increment();
obj2.increment();
obj3= obj2.Add(obj1);
cout<<"the first array is : " ;
obj1.print();
cout<<endl;
cout<<"the second array is : " ;
obj2.print();
cout<<endl;
cout<<"the third array is : " ;
obj3.print();
cout<<endl;
}
integerList::integerList()
{
counter=0;
for (int i=0;i<100;i++)
list[i]=0;
}
void integerList::insert(int a)
{
list[counter]=a;
counter++;
}
bool integerList::Find(int a)
{
bool exists;
for (int i=0;i<100;i++)
if (list[i]==a)
{
exists = true ;
break ;
}
else
exists= false ;
return exists;
}
bool integerList::isEmpty()
{
bool empty;
for (int i=0;i<100;i++)
if (list[i]!=0)
{
empty= false ;
break ;
}
else
empty = true ;
return empty;
}
bool integerList::isFull()
{
bool full;
for (int i=0;i<100;i++)
if (list[i]==0)
{
full=false ;
break ;
}
else
full= true ;
return full;
}
integerList integerList::Add(integerList & object)
{
integerList temp;
for (int i=0;i<100;i++)
temp.list[i]=list[i]+ object.list[i];
return object;
}
void integerList::increment()
{
for (int i=0;i<100;i++)
list[i]++;
}
void integerList::print()
{
for (int i=0;i<100;i++)
cout<<list[i]<<" " ;
cout<<endl;
}
but i still dont understand why
integerList integerList::Add(integerList & object)
isn't functioning properly
Mar 7, 2012 at 5:12pm UTC
You're passing by reference and returning a value. The object your passing into it is actually changing in the function, so the object it's being assigned probably won't have what you want it to.
Mar 7, 2012 at 5:21pm UTC
thank you so much ^_^
i can't believe i spent half an hour lookin at it and didnt notice that!!!
thx 4 the help ^_^
Topic archived. No new replies allowed.