CE241-Fall’08-Midterm#2
CE241 Programming Languages Course @KHas – Fall 2008 – Midterm #2 solutions and some extra code including linked list examples.
CE241 Programming Languages course description is available here.
Source files:
[ce241-fall08-midterm2-q2.cpp] | show
// ==================================================
// - File______: ce241-fall08-midterm2-q2.cpp _______
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: ??/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// function declarations
int strLenIter(const char *str);
int strLenRec(const char *str);
int main()
{
char str[] = "ankara";
cout << "strLenIter(" << str << ") = " << strLenIter(str) << endl;
cout << "strLenRec(" << str << ") = " << strLenRec(str) << endl;
return 0;
}
// function definitions
// iterative implementation of string length calculation
int strLenIter(const char *str)
{
int count = 0;
while (*str++ != '\0')
{
++count;
}
return count;
}
// recursive implementation of string length calculation
int strLenRec(const char *str)
{
// base case
if (*str == '\0')
{
return 0;
}
// general case
return strLenRec(str + 1) + 1;
}
[ce241-fall08-midterm2-q3.cpp] | show
// ==================================================
// - File______: ce241-fall08-midterm2-q3.cpp _______
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: ??/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct Student
{
int id;
int grade;
};
int main()
{
Student *pStuArr;
int stCount;
cout << "Enter # of students: ";
cin >> stCount;
// create a dynamic array
pStuArr = new Student[stCount];
// fill the array
for (int i = 0; i < stCount; ++i)
{
cout << "Enter id: ";
cin >> pStuArr[i].id;
cout << "Enter grade: ";
cin >> pStuArr[i].grade;
}
// iterate the array and modify array items
for (int i = 0; i < stCount; ++i)
{
pStuArr[i].grade += 10;
}
// iterate the array and print array items
for (int i = 0; i < stCount; ++i)
{
cout << pStuArr[i].grade << endl;
}
// delete the dynamic array
delete [] pStuArr;
return 0;
}
[ce241-fall08-midterm2-q4.cpp] | show
// ==================================================
// - File______: ce241-fall08-midterm2-q4.cpp _______
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: ??/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct Student
{
int id;
int grade;
Student *pNext;
};
// function declarations
void printListRev(const Student *pHead);
int countGradesLessThan50(const Student *pHead);
int main()
{
Student s1 = {1, 30, NULL};
Student s2 = {2, 40, NULL};
Student s3 = {3, 60, NULL};
s1.pNext = &s2;
s2.pNext = &s3;
s3.pNext = NULL;
cout << countGradesLessThan50(&s1) << endl;
printListRev(&s1);
return 0;
}
// function definitions
void printListRev(const Student *pHead)
{
if (pHead == NULL)
{
return;
}
printListRev(pHead->pNext);
cout << pHead->grade << endl;
}
int countGradesLessThan50(const Student *pHead)
{
if (pHead == NULL)
{
return 0;
}
if (pHead->grade < 50)
{
return countGradesLessThan50(pHead->pNext) + 1;
}
else
{
return countGradesLessThan50(pHead->pNext);
}
}
[gradeList-examples.cpp] | show
// ==================================================
// - File______: gradeList-examples.cpp _____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: ??/12/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct GradeNode
{
int id;
int grade;
GradeNode *pNext;
};
// function declarations
GradeNode *gradeListToArray(const GradeNode *pHead, int *pArrLength);
GradeNode *gradeArrayToList(const GradeNode gradeArr[], int arrLength);
GradeNode *newGradeNode(int id, int grade);
void appendGrade(GradeNode *pHead, int id, int grade);
void destroyGradeList(GradeNode *pHead);
int main()
{
GradeNode g1 = {1, 30, NULL};
GradeNode g2 = {2, 40, NULL};
GradeNode g3 = {3, 60, NULL};
g1.pNext = &g2;
g2.pNext = &g3;
g3.pNext = NULL;
// convert list to array
int arrLength;
GradeNode *pGrArr = gradeListToArray(&g1, &arrLength);
cout << "printing array..." << endl;
for (int i = 0; i < arrLength; ++i)
{
cout << pGrArr[i].id << ", ";
cout << pGrArr[i].grade << endl;
}
// convert array to list
GradeNode *pHead = gradeArrayToList(pGrArr, arrLength);
cout << "printing list..." << endl;
for (GradeNode *pNode = pHead; pNode != NULL; pNode = pNode->pNext)
{
cout << pNode->id << ", " << pNode->grade << endl;
}
// destroy both list and array
destroyGradeList(pHead);
delete [] pGrArr;
return 0;
}
// function definitions
GradeNode *gradeListToArray(const GradeNode *pHead, int *pArrLength)
{
GradeNode *pGradeArr;
int count = 0;
// count items of the list
for (const GradeNode *pNode = pHead; pNode != NULL; pNode = pNode->pNext)
{
++count;
}
// assign count to where pArrLength points
*pArrLength = count;
// create a dynamic array of size "count"
pGradeArr = new GradeNode[count];
// copy values from list to array
for (int i = 0; i < count; pHead = pHead->pNext, ++i)
{
pGradeArr[i].id = pHead->id;
pGradeArr[i].grade = pHead->grade;
}
// return dynamic array's address
return pGradeArr;
}
GradeNode *gradeArrayToList(const GradeNode gradeArr[], int arrLength)
{
// create first item of the list
GradeNode *pHead = newGradeNode(gradeArr[0].id, gradeArr[0].grade);
// create the rest of the list
for (int i = 1; i < arrLength; ++i)
{
appendGrade(pHead, gradeArr[i].id, gradeArr[i].grade);
}
// return the address of the list
return pHead;
}
GradeNode *newGradeNode(int id, int grade)
{
GradeNode *pGradeNode = new GradeNode;
pGradeNode->id = id;
pGradeNode->grade = grade;
pGradeNode->pNext = NULL;
return pGradeNode;
}
void appendGrade(GradeNode *pHead, int id, int grade)
{
if (pHead == NULL)
{
return;
}
GradeNode *pNewGradeNode = newGradeNode(id, grade);
GradeNode *pNode;
for (pNode = pHead; ; pNode = pNode->pNext)
{
if (pNode->pNext == NULL)
{
break;
}
}
pNode->pNext = pNewGradeNode;
}
void destroyGradeList(GradeNode *pHead)
{
// destroy the list and free all dynamically allocated memory
for (GradeNode *pNode = pHead; pNode != NULL; )
{
GradeNode *pTmpNode = pNode->pNext;
delete pNode;
pNode = pTmpNode;
}
}