CSC2402 | ASSIGNMENT 1 FIVE TASKS LINUX

HI6006 Competitive Strategy
MCIS 5103 | ADVANCED PROGRAMMING CONCEPTS
March 13, 2018
SITXHRM401 Roster Staff Assessment
March 14, 2018

CSC2402 | ASSIGNMENT 1 FIVE TASKS LINUX

HI6006 Competitive Strategy

CSC2402 | ASSIGNMENT 1 FIVE TASKS LINUX | LINUX

COMPUTER ASSIGNMENT HELP

 

Assignment 1 consists of five tasks. For each task, you have to submit a .cpp file and a documentation file. The .cpp file should have the program code and short comments (one line or two) about the algorithm and the coding. We may compile the .cpp files using MinGW 4.7.1 if required. The documentation files should contain the compilation messages and sample runs for each program. All files must be in pure text format. No PDF, HTML, Word files, Open Office files, RTF etc. You will submit a single ZIP file (not RAR) which has all the files for your assignment (the .cpp files and the documentation files).

If you are using Codelite, the compilation messages can be copied and pasted with the usual crtl-c and crtl-v. For output of sample runs, right click the title bar of the command window, select EDIT/MARK and then highlight the output. With the output selected, right click the title bar of the command window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor such as Notepad, and crtl-v will paste the copied output to the text file.

If you are using MinGW, right click the title bar of the command window; select EDIT/MARK and then high light the output. With the output selected, right click the title bar of the command window, select EDIT/COPY. Position your cursor to a (new) text file opened in a text editor such as Notepad, and crtl-v will paste the copied output to the text file.

If you are using Linux, you can cut and paste the output on the terminal window into a text such as vim.

Name the files as task_1_1.cpp, task_1_2.cpp, task_1_3.cpp, task_1_4.cpp, task_1_5.cpp, task_1_1.txt and task_1_2.txt, task_1_3.txt, task_1_4.txt and task_1_5.txt.

Other files you should download:

 

  1. cpp
  1. cpp
  1. cpp
  1. cpp
  1. cpp

Background information

Structure

Structure is one way to bundle data together in C++.

Suppose we want to manipulate a group of related data: name, age and salary of a person. We can create a data type (structure) called Person as shown in sample_structure.cpp.

#include <iostream>

using namespace std;

int main(){

struct Person {

string name;

int age;

double salary;

 

};

Person Peter;                                                     // Similar to string Peter;

Peter.name = “Peter the Great”;     // dot refers to member name

Peter.age = 44;

Peter.salary = 44.44;

 

cout << Peter.name << ” is ” << Peter.age

  • ” and salary is $” << Peter.salary << endl; return 0;

 

}

Pointers

We can pass parameters by value or by reference. There is a third way, by pointer (reference) which works in a similar fashion as passing by reference. When passed by value, a copy is passed and the original data is not changed by the function call. Passing by pointer / reference allows the called function to modify the original value of the variable passed. Sample_passing.cpp illustrates the cases. See textbook chapter 7, section 7.1 to 7.3 for more details.

#include <iostream>

using namespace std;

void funct1(int);

void funct2(int&);

 

void funct3(int*);

int main(){

int n = 100;                        // integer

int * iptr1;                        // pointer

int * iptr2;

  • 100 will be printed cout << n << endl;
  • address of int is a pointer to the same int iptr1 = &n;
  • value pointed at by iptr will be printed cout << *iptr1 << endl;
  • create an unnamed int and point to it with iptr2 iptr2 = new int(300);

cout << *iptr2 << endl;

  • pass by value, 101 will be printed

funct1(n);

  • n not changed, 100 will be printed cout << n << endl;
  • pass by reference, 101 will be printed funct2(n);
  • n changed, 101 will be printed

cout << n << endl;

 

  • pass by pointer (reference), 301 will be printed funct3(iptr2);
  • n changed, 301 will be printed

cout << *iptr2 << endl;

return 0;

}

void funct1(int i){

 

i += 1;

cout << “Inside funct1, i = ” << i << endl;

}

  • the ‘&’ appearing after the parameter type indicates
  • that it is passed by reference

void funct2(int& i){

  • same as pass by value i += 1;

cout << “Inside funct1, i = ” << i << endl;

 

}

void funct3(int* i){

*i += 1;                     // *i instead of i

cout << “Inside funct1, i = ” << *i << endl;

}

<ctime> library

C++ provides a date-time library <ctime> for handling date-time. You can get the system date-time using the library function time() which returns the current calendar time. We can break down the calendar time to different date-time components using localtime() which returns local date-time information in a date-time structure.

Date-time structure:

struct tm {

 

int tm_sec // seconds; 0-59
int tm_min // minutes; 0-59
int tm_hour // hour of the day; 0-23
int tm_mday // day of month; 1-31
int tm_mon // month since January; 0-11
int tm_year // year since 1900;
int tm_wday // day of week; 0-6 (Sun, … Sat)
int tm_yday // day since 1 January; 0-365
int tm_isdst // daylight saving status;
  // > 0 if operational;
  // == 0 not operational;
  // < 0 no information available.

 

}

We can retrieve any one of the date-time structure member using strftime(). Options for strftime()

and the range of returned values:

Option Returned value Range of
returned value
   
     
a Abbreviated weekday Thu
A Full weekday Thursday
     
b Abbreviated month Aug
B Full month name August
     
c Local date and time Thu Aug 23 13:33:02 2017
d Day of month 01-31
H Hour 00-23
     
I Hour 01-12
     
j Day of the year 001-366
     
m Month 01-12
M Minute 00-59
     
p AM or PM PM
S Second 00-59
U U week number of the year; 00-53
Sunday as first day of week  
   
w Weekday 0-6, Sunday is 0
     
W Week number of the year; 00-53
Monday as first day of week  
   
     
x Local date representation 02/23/17
X Local time representation 14:45:02
     
y Year without century 00-99
     
Y Year with century 2017
     
Z Time zone (100 = 1 hour) +100
     

 

Sample_time_A.cpp illustrates the use of the library functions to get the current time:

#include <iostream>

#include <ctime>

using namespace std;

int main(){

  • calendar time time_t rawtime;
  • get the current calendar time time( &rawtime );
  • date-time structure pointer struct tm *timeinfo;
  • break the calendar time to components timeinfo = localtime ( &rawtime );
  • retrieve individual date-time structure members
  • using option “%a” and option “%A”.

char ans_abr[40];          // Character array

char ans_full[40];

strftime(ans_abr, 40, “%a”, timeinfo);

strftime(ans_full, 40, “%A”, timeinfo);

  • view the formatted date-time structure member values cout << ” Abbreviated weekday name: ” << ans_abr << endl;

cout << ” Full weekday name: ” << ans_full << endl; return 0;

 

}

We can also create a date-time structure using our own year, day, month, hour, minute and second information. Before we can use it as a valid date-time structure, we have to call mktime() to adjust other members of the date-time structure to make it valid. Sample_time_B.cpp illustrates the use of the library functions to specify a time

#include <iostream>

#include <ctime>

 

using namespace std;

int main(){

  • date-time structure struct tm tmStruct; int y = 2013;
int m = 4; // April
int d = 1; // my lucky day
int h = 14; // 2 pm
int min = 30; // half past
int s = 3; // who cares?

 

  • initialize/modify the date-time structure

tmStruct.tm_year = y – 1900;  // Legacy problem

tmStruct.tm_mon = m – 1;                // month starts from 0 not 1

tmStruct.tm_mday = d;

tmStruct.tm_hour = h-1;

tmStruct.tm_min = min-1;

tmStruct.tm_sec = s-1;

  • call mktime() to adjust other members of tmStruct mktime ( &tmStruct );
  • using option “%a” and option “%A”.

char ans_localtime[40]; // Character array char ans_full[40];

strftime(ans_localtime, 40, “%c”, &tmStruct); strftime(ans_full, 40, “%A”, &tmStruct);

  • view the formatted date-time structure member values cout << “Local date time is: ” << ans_localtime << endl; cout << ” Full weekday name: ” << ans_full << endl; return 0;

}

Task 1 (15 marks)

Write a C++ program that is to be used for a local taxi fare calculation. The rules regarding the calculations are as follows. (Note: American spelling for Kilometer in specification)

The fare is calculated based on distance travelled and time taken to travel the distance.

The services charges $2.00 for the first 2 kilometre of travel (even if you are under 2 kilometers). After the first 2 kilometers the services charges $0.50 cents for each additional kilometer up to additional 6 kilometers. After those 6 kilometers the customer is charged a $1.00 for each additional kilometers

At the end of the ride, a fee of $0.20 cents is charged based on the total minutes travel time.

The total fare is the journey cost added to the travel time cost.

The application is then to print out a personalised invoice for the journey as specified below.

Your application needs to display a prompt for information to be inputted by the user.

Input from the keyboard is for whole numbers only (int).

 

C:>task_1_1

Please enter your name: Peter Johnson

Please enter distance travelled as km: 12

Please enter duration of journey in minutes: 16

 

Hello, Peter!

Your journey of 12 kilometers and 16 minutes comes to $12.20

Travel cost $9.00 for distance of 12 kilometers Time cost $3.20 for 16 minutes travel time.

C:>task_1_1

Please enter your name: Green Pumpkin

Please enter distance travelled as km: 1

Please enter duration of journey in minutes: 13

 

Hello, Green!

Your journey of 1 kilometers and 13 minutes comes to $4.60

Travel cost $2.00 for distance of 1 kilometers Time cost $2.60 for 13 minutes travel time.

C:>task_1_1

Please enter your name: Tom Green

Please enter distance travelled as km: 21

Please enter duration of journey in minutes: 8

Hello, Tom!

Your journey of 21 kilometers and 8 minutes comes to $19.60

Travel cost $18.00 for distance of 21 kilometers Time cost $1.60 for 8 minutes travel time.

User will be able to enter names with multiple words. Your code should be able to capture names consisting of any number of words (hundreds, at least). You should record the compilation message(s) even though there may be none. Run the program as shown in the sample runs. Put all compilation messages and sample run outputs in task_1_1.txt.

Marking Criteria Marks
Program can only handle multiple-word names 1
Program calculates journey cost correctly 3
Program calculates time cost correctly 2
Program displays the messages correctly 3
Code factored into functions for reuse 5
Compilation messages and sample run outputs (if the program runs correctly) 1

 

Task 2 (15 marks)

Write a C++ program task_1_2.cpp which prompts for an integer, validate the integer, and if not valid, ask again till a valid integer is entered. Repeat for another five integers with different validation criteria.

Your solution source code is to be only the main() function. The whole logic for the solution needs to reside in the main routine.

Sample outputs are:

 

C:> a      
Please enter the year: 2013
Please enter the month: 1
Please enter the day: 24
Please enter the hour: 13
Please enter the minute: 55
Please enter the second: 45
Your inputs are:  
Year   = 2013    
Month   = 1    
Day   = 24    
Hour   = 13    
Minute = 55    
Second = 45    
C:>a        
Please enter the year: 1969
1970 <= Year <= 2020!
Please enter the year: 2034
1970 <= Year <= 2020!
Please enter the year: Hello and bye
1970 <= Year <= 2020!
Please enter the year: 2013
Please enter the month: 0
1 <= Month <= 12!  
Please enter the month: 13
1 <= Month <= 12!  
Please enter the month: Hello and bye
1 <= Month <= 12!  
Please enter the month: 1
Please enter the day: 0
1 <= Day<= 31!  
Please enter the day: 32
1 <= Day<= 31!  
Please enter the day: Hello and bye
1 <= Day<= 31!  
Please enter the day: 24
Please enter the hour: -1
0 <= Hour<= 23!  
Please enter the hour: 24
0 <= Hour<= 23!  
Please enter the hour: Hello and bye
0 <= Hour<= 23!  
Please enter the hour: 13
Please enter the minute: -1
0 <= Minute <= 59!
Please enter the minute: 60
0 <= Minute <= 59!
Please enter the minute: Hello and bye

0 <= Minute <= 59!

Please enter the minute: 30

Please enter the second: -1

1 <= Second<= 59!

Please enter the second: 60

1 <= Second<= 59!

Please enter the second: Hello and bye 1 <= Second<= 59!

Please enter the second: 35

 

Your inputs are:

Year        = 2013

Month  = 1

Day          = 24

Hour        = 13

Minute = 30

Second = 35

The name of the six integers and their validation criteria are shown below:

Name Criteria
year 1970 <= year <= 2020
month 1 <= month <= 12
day 1 <= day <= 31
hour 0 <= hour <= 23
min 0 <= min <= 59
sec 0 <= sec <= 59

You should record the compilation message(s) even though there may be none. Run the program as shown in the sample runs. Put all compilation messages and sample run outputs in task_1_2.txt.

Marking Criteria Marks
Data input 1
Data validation 3
Looping for a correct input 4
Handle error input with appropriate output message 4
Solution uses main function only for the code 2
Compilation messages and sample run outputs (if the program runs correctly) 1

Task 3 (20 marks)

Write second version of Task 2 where you reflector the code to use functions as specified below.

Your code in Task 2 for entering each integer is the same except the prompts, variable names and the validation criteria.

Group the code for entering an integer to form a function void get_data(). The parameters for the function void get_data()are:

the prompt for user input;

the prompt for correct range of input;

integer variable used to store the user input;

criteria 1 – input should be smaller than or equal to this value; criteria 2 – input should be greater than or equal to this value;

void get_data() returns control to caller only when the input data is valid.

Write a C++ program task_1_3.cpp calls get_data() to get 6 valid integers. The validation criteria and sample output should be the same as shown in Task 2.

You should record the compilation message(s) even though there may be none. Run the program as shown in the sample runs for task 2. Put all compilation messages and sample run outputs in task_1_3.txt.

Marking Criteria Marks
Function declaration 4
Function definition 7
Parameter passing to functions 7
Looping for correct input 1
Compilation messages and sample run outputs (if the program runs correctly) 1

Task 4 (20 marks)

Write a C++ program task_1_4.cpp which uses a function void get_option(some_parameter) to get user input for a char option. The char must be an alphanumeric character i.e. alphabets and digits. If the input is not valid, void get_option() will be called until a valid user input is obtained.

The validated option will then be used as a parameter in calling a function string test(). Function string test() will display different strings according to the value of option.

Value of option can only be:

a digit (use isdigit(char) in <cctype>)

o     char option = ‘1’;     isdigit(option) will return true;

  • char option = ‘a’; isdigit(option) will return false; a lower case letter (use islower(char) in <cctype>)

o     char option = ‘a’;     islower(option) will return true;

  • char option = ‘A’; islower(option) will return false; an upper case letter (use isupper(char) in <cctype>)

o char option = ‘A’; isupper(option) will return true; o char option = ‘1’; isupper(option) will return false;

test() is shown below:

string test(char option){

if (isupper(option)) return “An upper case letter is entered!”;

if (islower(option)) return “A lower case letter is entered!”;

return “A digit is entered!”;

 

}

Sample outputs:

C:>a

Please enter an alphanumeric char: &

Please enter an alphanumeric char: (

Please enter an alphanumeric char: ?

 

Please enter an alphanumeric char: 2

A digit is entered!

C:>a

Please enter an alphanumeric char: a

A lower case letter is entered!

 

C:>a

Please enter an alphanumeric char: A

An upper case letter is entered!

You should record the compilation message(s) even though there may be none. Run the program as shown in the sample runs shown above. Put all compilation messages and sample run outputs in task_1_4.txt.

Marking Criteria Marks
Function declaration 4
Function definition 5
Passing parameters 5
Looping for correct input 1
Calling test() 4
Compilation messages and sample run outputs (if the program runs correctly) 1

 

Task 5 (30 %)

Compile and run sample_5.cpp. Study the program alongside with the output and make sure you understand how to use the time library functions.

Write a C++ program which:

Prompts user for inputting his name consisting of multiple words;

Prompts user for inputting valid int year, int month, int day, int hour, int minute, int second.

o  The validation criteria is the same as in task 2.

Prompts user to input 4 options for calls to get_string_component() .

  • Option can only be one of the following: a abbreviated weekday

A full weekday

b abbreviated month B full month

c local date and time d day of month (01-31) H hour (00-23)

I hour (1-12)

j day of the year (001-366) m month (01-12)

M minute (00-59) p Am or PM

S second (00-59)

U week numberof the year (00-53); Sunday as first day of week

w weekday  (0-6, Sunday is 0)

W week numberof the year (00-53);

Monday as first day of week

x local date

X local time

  • year without century (00-99) Y year with century

Z time zone.

Constructs a date-time structure with the date time information just entered;

Outputs user name, local time, abbreviated weekday, and the day of the year for the date-time using the user name and appropriate options (‘c’, ‘a’, ‘j’, ‘Y’) for get_string_component().

 

Sample output:

C:work_directoryCSC2402S1_2013assignmentS1_

Please enter your name: Peter Johnson Junior

Please enter the year: 2013

Please enter the month: 1

Please enter the day: 24

Please enter the hour: 2

Please enter the minute: 4

Please enter the second: 6

Please enter your first option: c

Please enter your second option: a

Please enter your third option: j

Please enter your fourth option: Y

You should record the compilation message(s) even though there may be none. Run the program as shown in the sample runs shown above. Put all compilation messages and sample run outputs in task_1_5.txt.

Marking Criteria Marks
Get user name using function call 1
Get date-time information using function call 1
Get options using function call 1
Date-time structure 6
Get localtime, weekday, days of the year 6
Correct Output 5
Compilation messages and sample run outputs (if the program runs correctly) 10

 

Punjab Assignment Help

Buy Online Assignment Help services for COMPUTER ASSIGNMENT with Punjab Assignment Help at punjabassignmenthelp@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *