Please help me with this.... C++ ***********************************************
ID: 3791406 • Letter: P
Question
Please help me with this.... C++
************************************************************************************
Program 1 - Implementing ADT Class with individual variables
What are the Pors and Cons with implementing with the LIST with individual variables ?
Let’s assume that you have at most ten(10) integer variable of data you need to store in a list, whose value would be greater or equal to zero.
int count;
= v5 = v6 = v7 = v8 = v9 = v10 = -1; count =
if (v1 == -1) { v1 = inVar; }
else if (v2 == -1) { v2 = inVar; return true; } else if (v3 == -1) { v3 = inVar; return true; } else if (v4 == -1) { v4 = inVar; return true; } else if (v5 == -1) { v5 = inVar; return true; } else if (v6 == -1) { v6 = inVar; return true; } else if (v7 == -1) { v7 = inVar; return true; } else if (v8 == -1) { v8 = inVar; return true; } else if (v9 == -1) { v9 = inVar; return true; } else if (v10 == -1) { v10 = inVar; return true; } else return false;
{ return false; }
} // called individually and also by the add method
bool found(int inVal) { // called individually and also by the delete method
if (v1 == inVal) { v1 = -1; return true; }
else if (v2 == inVal) { v2 = -1; return true; } else if (v3 == inVal) { v3 = -1; return true; } else if (v4 == inVal) { v4 = -1; return true; } else if (v5 == inVal) { v5 = -1; return true; } else if (v6 == inVal) { v6 = -1; return true; } else if (v7 == inVal) { v7 = -1; return true; } else if (v8 == inVal) { v8 = -1; return true; } else if (v9 == inVal) { v9 = -1; return true; } else if (v10 == inVal) { v10 = -1; return true; } else { return false; }
return false;
}
// others
};
int main() {
cout << "Hello world!" << endl; // Test all access methods... LISTofIntVars L1;
L1.add1(42);
L1.add1(17);
) { cout << "17 found" << endl;} else { cout << "17 not found" << ) { cout << "42 found" << endl;} else { cout << "14 not found" << makeEmpty function is Not yet coded
return 0; }
Program 2 – Implement the same program with an array of 10 integers.
Program 3 – Implement the LIST using pointers to store data for an unlimited number of integer values.
Program 4 - Create a LIST using the STL List that stores integers.
Explanation / Answer
Program-1:
From the program-1, By taking 10 individual variables seperatley, it leads to ambiguity and tedius task for debug and process the program.
Pros:
Readibility
Easy to write
Cons:
Ambiguity
Tedius
Non Maintainibility
Fault Tolerance for registers to store variables
Interrupts may occur
Program-2:
#include <iostream>
using namespace std;
int main()
{
const int size = 10; //array size
int a[size] = {1,2,3,4,5,6,7,8,9,10}; //array with 10 elements
cout << "Displaying Array..." << endl;
//for loop that iterates through the while array and display all its elements
for(int i = 0; i < size; i++)
{
//array[i] represent ith element in the array.e.g 0th element is 32, 4th element is 78 etc
cout << "a[" << i << "] => " << a[i] << endl;
}
return 0;
}
Output:
Displaying Array...
a[0] => 1
a[1] => 2
a[2] => 3
a[3] => 4
a[4] => 5
a[5] => 6
a[6] => 7
a[7] => 8
a[8] => 9
a[9] => 10
Program-3
#include <iostream>
using namespace std;
int main()
{
int *ptr;
int n;
cout<<"Enter Size:";
cin>>n;
ptr = new int[n]; // Allocating memory for pointer
cout<<"Enter the elements:"<<endl;
for(int i=0;i<n;i++){
cin>>*(ptr+i); // Reading elements into pointer
}
cout<<"The elements are:"<<endl;
for(int i=0;i<n;i++){
cout<<*(ptr+i)<<" "; //Printing elements from pointer
}
return 0;
}
Output:
Enter the elements:
1 2 3 4 5
The elements are:1 2 3 4 5
Program 4 - Create a LIST using the STL List that stores integers.
#include <iostream>
#include <list>
using namespace std;
int main ()
{
std::list<int> l; // List Object of type integers
int i;
std::cout << "Enter Few integers (enter 0 to end): ";
do {
std::cin >> i;
l.push_back (i); // pushing into list
} while (i);
std::cout << "List stores " << l.size() << " numbers. ";
for (std::list<int>::iterator it=l.begin(); it != l.end(); ++it) // Iterator for iterating list data
std::cout << ' ' << *it; // Printing Iteratot
return 0;
}
Output
Enter Few integers (enter 0 to end):
1 2 3 4 5 0
List stores 6 numbers.
1 2 3 4 5 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.