CE241-Fall’08-Homework#7
CE241 Programming Languages Course @KHas – Fall 2008 – Homework #7 questions, solutions and some extra code written in the lab in order to introduce linked lists.
CE241 Programming Languages course description is available here.
Download questions and solutions
Source files:
[ce241-fall08-hw7-q1.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw7-q1.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 04/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function declarations
void printArrayInt(const int *pArray, int size);
int main()
{
int *pArray;
int arraySize;
// initialize random number generation library's seed value
srand((unsigned int) time(0));
// get array size
cout << "Enter array size: ";
cin >> arraySize;
// allocate memory for dynamic array
pArray = new int[arraySize];
// initialize the array with random values
for (int i = 0; i < arraySize; ++i)
{
pArray[i] = rand() % 100;
}
// print the array content
printArrayInt(pArray, arraySize);
cout << endl;
// deallocate the memory we allocated for the array
delete [] pArray;
return 0;
}
// function definitions
void printArrayInt(const int *pArray, int size)
{
while (size--)
{
cout << *pArray++ << " ";
}
}
[ce241-fall08-hw7-q2.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw7-q2.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 04/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const char *str = "ali_veli";
char *strDynamic;
int size;
// get character array size
cout << "Enter size: ";
cin >> size;
// allocate memory for the dynamic string
strDynamic = new char[size];
// initialize the string by copying str to it
strncpy(strDynamic, str, (size - 1));
strDynamic[size - 1] = '\0';
// print the string
cout << strDynamic << endl;
// deallocate the memory we allocated for the string
delete [] strDynamic;
return 0;
}
[ce241-fall08-hw7-q3.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw7-q3.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 04/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct ProjectMember
{
int memberId;
char *memberName;
int contributionNHours;
};
// symbolic constants
const int N_MEMBERS = 4;
int main()
{
ProjectMember members[N_MEMBERS] =
{
{1, "Ali", 12},
{2, "Veli", 25},
{3, "Hasan", 32},
{4, "Deniz", 8 }
};
int completionTime = 0;
// calculate total project completion time
for (int i = 0; i < N_MEMBERS; ++i)
{
completionTime += members[i].contributionNHours;
}
cout << "Total completion time is: " << completionTime << " hours." << endl;
// print project members' names
cout << endl << "Project members:" << endl;
for (int i = 0; i < N_MEMBERS; ++i)
{
cout << members[i].memberName << endl;
}
return 0;
}
[ce241-fall08-hw7-q4.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw7-q4.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 04/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstring>
using namespace std;
// struct declaration
struct BirthDate
{
char *nameOfFriend;
int day;
int month;
int year;
};
// function declarations
int findBirthDateByName(const BirthDate *pBirthDates, int size, const char *name);
// symbolic constants
const int N_BIRTHDATES = 5;
int main()
{
BirthDate birthDates[N_BIRTHDATES] =
{
{"Ali", 1, 2, 1980},
{"Veli", 3, 4, 1982},
{"Hasan", 5, 6, 1984},
{"Yeliz", 7, 8, 1986},
{"Kaan", 9, 1, 1988}
};
int idx;
idx = findBirthDateByName(birthDates, N_BIRTHDATES, "Yeliz");
if (idx == -1)
{
cout << "cannot find name..." << endl;
}
else
{
cout << "name found... birth date is: ";
cout
<< birthDates[idx].day << "/"
<< birthDates[idx].month << "/"
<< birthDates[idx].year << endl;
}
idx = findBirthDateByName(birthDates, N_BIRTHDATES, "Deniz");
if (idx == -1)
{
cout << "cannot find name..." << endl;
}
else
{
cout << "name found... birth date is: ";
cout
<< birthDates[idx].day << "/"
<< birthDates[idx].month << "/"
<< birthDates[idx].year << endl;
}
return 0;
}
// function definitions
int findBirthDateByName(const BirthDate *pBirthDates, int size, const char *name)
{
for (int i = 0; i < size; ++i)
{
if (strcmp(pBirthDates[i].nameOfFriend, name) == 0)
{
return i;
}
}
return -1;
}
[gradeList.cpp] | show
// ==================================================
// - File______: gradeList.cpp ______________________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 05/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct GradeNode
{
int studId;
int grade;
GradeNode *pNext;
};
int main()
{
GradeNode g1, g2, g3, g4;
// construct the list
g1.pNext = &g2;
g2.pNext = &g3;
g3.pNext = &g4;
g4.pNext = NULL;
// assign values to grades by iterating through the list
for (GradeNode *pGrade = &g1; pGrade != NULL; pGrade = pGrade->pNext)
{
pGrade->grade = 90;
}
cout << "Grades: " << endl;
// print grades by iterating through the list
for (GradeNode *pGrade = &g1; pGrade != NULL; pGrade = pGrade->pNext)
{
cout << pGrade->grade << endl;
}
cout << endl;
return 0;
}
[gradeList-dynamic.cpp] | show
// ==================================================
// - File______: gradeList-dynamic.cpp ______________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 05/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstdlib>
using namespace std;
// struct declaration
struct GradeNode
{
int studId;
int grade;
GradeNode *pNext;
};
int main()
{
GradeNode *pFirst, *pLast;
int listSize;
cout << "Enter list size: ";
cin >> listSize;
// create the first node of the list
pFirst = new GradeNode;
pFirst->pNext = NULL;
pLast = pFirst;
// construct the remaining nodes of the list
// loop will iterate (listSize - 1) times
while (--listSize)
{
GradeNode *pTmp = new GradeNode;
pTmp->pNext = NULL;
pLast->pNext = pTmp;
pLast = pTmp;
}
// assign grades by iterating through the list
for (GradeNode *pGrade = pFirst; pGrade != NULL; pGrade = pGrade->pNext)
{
cout << "Enter a grade(0-100):";
cin >> pGrade->grade;
}
// print grades by iterating through the list
cout << endl << "Grades:" << endl;
for (GradeNode *pGrade = pFirst; pGrade != NULL; pGrade = pGrade->pNext)
{
cout << pGrade->grade << endl;
}
cout << endl;
// deallocate the GradeNode structs we allocate while constructing the list
for (GradeNode *pGrade = pFirst; pGrade != NULL; )
{
GradeNode *pNextTmp = pGrade->pNext;
delete pGrade;
pGrade = pNextTmp;
}
return 0;
}