CE241-Fall’08-Homework#5
CE241 Programming Languages Course @KHas – Fall 2008 – Homework #5 questions and solutions.
CE241 Programming Languages course description is available here.
Download questions and solutions
Source files:
[ce241-fall08-hw5-q1.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw5-q1.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 21/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// symbolic constants
const int N_COURSES = 4;
const int N_EXAMS = 3;
// function declarations
void calculateAverages(const int grades[][N_EXAMS], double *pAverages, int nCourses);
int main()
{
double averages[N_COURSES];
int gradesTable[N_COURSES][N_EXAMS] =
{
{60, 70, 80},
{70, 80, 90},
{80, 90, 95},
{50, 60, 70}
};
// calculate averages
calculateAverages(gradesTable, averages, N_COURSES);
// print calculated averages
for (int i = 0; i < N_COURSES; ++i)
{
cout << "Average for Course-" << (i + 1) << ": " << averages[i] << endl;
}
return 0;
}
// function definitions
void calculateAverages(const int grades[][N_EXAMS], double *pAverages, int nCourses)
{
int tmpSum;
// traverse rows as courses
for (int i = 0; i < nCourses; ++i)
{
tmpSum = 0;
// calculate sum of exam grades for ith course
for (int k = 0; k < N_EXAMS; ++k)
{
tmpSum += grades[i][k];
}
// calculate average of exam grades for ith course
pAverages[i] = (double) tmpSum / N_EXAMS;
}
}
[ce241-fall08-hw5-q2.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw5-q2.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 21/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// function declarations
int stringLength(const char *str);
int main()
{
const char *strName = "Ali_Veli";
cout << "stringLength(" << strName << ") = " << stringLength(strName) << endl;
return 0;
}
// function definitions
int stringLength(const char *str)
{
int count = 0;
// count characters until null character
//while (*str++)
//{
// ++count;
//}
// count characters until null character
//while (*str)
//{
// ++str;
// ++count;
//}
// count characters until null character
//while (*str++ != '\0')
//{
// ++count;
//}
// count characters until null character
while (*str != '\0')
{
++str;
++count;
}
return count;
}
[ce241-fall08-hw5-q3.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw5-q3.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 21/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstring>
using namespace std;
// function declarations
bool isPalindromeRec(const char *str, int leftIdx, int rightIdx);
int main()
{
const char *strPal = "desserts | stressed";
if (isPalindromeRec(strPal, 0, (strlen(strPal) - 1)))
{
cout << "'" << strPal << "' is a palindrome...";
}
else
{
cout << "'" << strPal << "' is not a palindrome...";
}
cout << endl;
return 0;
}
// function definitions
bool isPalindromeRec(const char *str, int leftIdx, int rightIdx)
{
// base case
if (leftIdx > rightIdx)
{
return true;
}
// general case
if (str[leftIdx] == str[rightIdx])
{
return isPalindromeRec(str, (leftIdx + 1), (rightIdx - 1));
}
return false;
}
[ce241-fall08-hw5-q4.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw5-q4.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 21/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// function declarations
void strCopyReplace(const char *src, char *dest, char oldChar, char newChar);
int main()
{
const char *strSrc = "ankara_adana";
char strDest[20];
strCopyReplace(strSrc, strDest, 'a', 'X');
cout << "Original string: " << strSrc << endl;
cout << "Modified string: " << strDest << endl;
return 0;
}
// function definitions
void strCopyReplace(const char *src, char *dest, char oldChar, char newChar)
{
int i;
// copy the string from src to dest
for (i = 0; src[i] != '\0'; ++i)
{
// if current character is oldChar then copy newChar instead of it,
// otherwise just copy it.
if (src[i] == oldChar)
{
dest[i] = newChar;
}
else
{
dest[i] = src[i];
}
}
// put the null character to mark the end of the string
dest[i] = '\0';
}