Free Web Hosting by Netfirms
Web Hosting by Netfirms | Free Domain Names by Netfirms

Quiz 5


Test 1
Name: ClassyTime: 20 - 30min
Create a class which contains - Name & age in the private bit, then make an array of 10 of these classes and have a function in the class which updates all of the info when asked. The user will be asked to update a record of their choice > 1 -10 and then will type in the values which will be transfered over to the class. This process will be repeated until the user wishes to exit, no menu is required.


Test 2
Name: PointyTime: 20min
First create 2 variables - a & b, assign them both a value, then output there respective memory locations and see if they have been stored 1 after the other. Then create an array of 10 integers and output each elements memory location and see if they are stored sequentially.
Hint: There are many ways of doing this, but try to use pointers. Memory locations are given in Hexadecimal form, which means it will count up to 15 (0-15) then go back to 0 unlike our number system (0-9). Just see the last values on the returned address, forget the first few numbers and symbols.



Answers
Answer below - drag the cursor over the white space to highlight the answer
Test 1Classy Program , Source code
 
#include <conio.h>
#include <iostream.h>
#include <string.h>

class My_class
{
private:
	char Name[20];
	int Age;
public:
	void Set_values(char a[20],int b)
	{
		strcpy(Name,a);
		Age=b;
	}
};

void main()
{
	My_class Profiles[10];
	int Age,Record;
	char Name[20],Key='n';
	while((Key!='e')&&(Key!='E'))
	{
		cout << "Which record do you wish to enter 1-10: ";
		cin >> Record;
		cout << "Please enter person " << Record << " age: ";
		cin >> Age;
		cout << "Please enter person " << Record << " name: ";
		cin >> Name;
		Profiles[Record].Set_values(Name,Age);
		cout << "\n\nDo you wish to (e)xit or enter a (n)ew record: ";
		Key=getch();
		while((Key!='e')&&(Key!='n')&&(Key!='N')&&(Key!='E'))
		{
			cout << "\nError: re-enter your choice - e or n: ";
			Key=getch();
		}
		clrscr();		
	}
}
 
Test 2Pointy Program , Source code
 
#include <conio.h>
#include <iostream.h>

void main()
{
	int a,b,c[10],*Pointer,i;
	a=89;b=56;
	cout << "a is at: " << &a << " b is at: " << &b << endl << endl;
	Pointer = &c[0];
	for(i=0;i<10;i++)
	{
		cout << "Element 0: " << Pointer << endl;
		Pointer++;
	}
	getch();
}



Back Home Next