CE241-Fall’08-Homework#6
CE241 Programming Languages Course @KHas – Fall 2008 – Homework #6 questions and solutions.
CE241 Programming Languages course description is available here.
Download questions and solutions
Source files:
[ce241-fall08-hw6-q1.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw6-q1.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 27/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct Time
{
int hour;
int minute;
int second;
};
int main()
{
Time t;
cout << "Enter hour: ";
cin >> t.hour;
cout << "Enter minute: ";
cin >> t.minute;
cout << "Enter second: ";
cin >> t.second;
cout << "Time: " << t.hour << ":" << t.minute << ":" << t.second << endl;
return 0;
}
[ce241-fall08-hw6-q2.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw6-q2.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 27/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
using namespace std;
// struct declaration
struct Time
{
int hour;
int minute;
int second;
};
// function declarations
bool TimeIsEqual(const Time *pTime1, const Time *pTime2);
int main()
{
Time time1 = {12, 32, 27};
Time time2 = {12, 32, 28};
Time time3 = {12, 32, 27};
// compare time1 and time2
if (TimeIsEqual(&time1, &time2))
{
cout << "time1 and time2 are equal...";
}
else
{
cout << "time1 and time2 are not equal...";
}
cout << endl;
// compare time1 and time3
if (TimeIsEqual(&time1, &time3))
{
cout << "time1 and time3 are equal...";
}
else
{
cout << "time1 and time3 are not equal...";
}
cout << endl;
return 0;
}
// function definitions
bool TimeIsEqual(const Time *pTime1, const Time *pTime2)
{
return (pTime1->hour == pTime2->hour) &&
(pTime1->minute == pTime2->minute) &&
(pTime1->second == pTime2->second);
}
[ce241-fall08-hw6-q3.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw6-q3.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 27/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstring>
using namespace std;
// function declarations
void printWordsReverse(const char *str);
void printNChars(const char *str, int nChars);
int main()
{
const char *str = "print me now";
printWordsReverse(str);
cout << endl;
return 0;
}
// function definitions
void printWordsReverse(const char *str)
{
int count = 0;
int idx = strlen(str) - 1;
while (true)
{
if (str[idx] == ' ')
{
printNChars((str + idx + 1), count);
cout << " ";
count = 0;
}
else if (idx == 0)
{
printNChars(str, (count + 1));
return;
}
else
{
++count;
}
--idx;
}
}
void printNChars(const char *str, int nChars)
{
while (nChars--)
{
cout << *str++;
}
}
[ce241-fall08-hw6-q4.cpp] | show
// ==================================================
// - File______: ce241-fall08-hw6-q4.cpp ____________
// - Author____: Ahmet Ardal ________________________
// - Contact___: ardalahmet@hotmail.com _____________
// - Date______: 27/11/2008 _________________________
// __________________________________________________
// - Copyright (c) 2008, Some Rights Reserved _______
// ==================================================
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// struct declaration
struct Grade
{
int studentId;
int grade;
};
// symbolic constants
const int N_STUDENTS = 30;
int main()
{
Grade gradesList[N_STUDENTS];
double average;
int gradesSum = 0;
// initialize random number generation library's seed value
srand((unsigned int) time(0));
// randomly generate student ids and grades
for (int i = 0; i < N_STUDENTS; ++i)
{
gradesList[i].studentId = rand();
gradesList[i].grade = rand() % 101;
}
// sum all grades
for (int i = 0; i < N_STUDENTS; ++i)
{
gradesSum += gradesList[i].grade;
}
// calculate and print the average
average = (double) gradesSum / N_STUDENTS;
cout << "Average is:" << average << endl;
return 0;
}