Starting from:

$30

EC327-Homework 4 Solved

Total: 100 points 


Q1. Multi dimensional arrays. 
 

Given a NxN matrix of floats, write a function getNorm that returns its 1-norm, defined as: 

 

The prototype of getNorm should be  

float getNorm(float **matrix, int N);
 

You can use any libraries that are included in the C++ installation on the lab machines, for example cmath. You are given a header file Q1.h and a sample main file Q1main.cpp. You can use them to test your code, which you should include in a file named Q1.cpp.  

 

Files to submit: Q1.cpp

 

Q2. Dynamic memory allocation. 
 

Write a function reverseString that, given a C++ string, returns a pointer to a new string whose content is the reverse of the original one. The prototype of reverseString is as follows:

 

 string* reverseString(string &s);
 

For example, when given a string with “EC327” as a value, reverseString will return a new string containing “723CE”.

 

You are given a header file Q2.h and a main file Q2main.cpp. Implement your function in a file called Q2.cpp and submit it.

 

Files to submit: Q2.cpp

 

Q3 Class design. 
 

a) Design a class named Student that contains: 

•       A string data field named first, which holds the student’s first name 

•       A string data field named last, which holds the student’s last name 

•       An unsigned integer data field id, which holds the student’s ID 

•       A float gpa, which holds the student’s GPA 

 

first, last, and id should be declared as public members, while gpa should be made private. 

 

•       A no argument constructor, which sets “Jane” as first, “Doe” as last, 0 as id, and 0.0 as gpa. 

•       A constructor that takes four arguments and assigns their values to first, last, id, and gpa respectively. The prototype of this constructor should be: 

◦ Student(string f, string l, int i, float g); 

 

•       Get and set functions to handle the private data field gpa, as follows: 

◦ void setGpa(float g); //assigns the value g to gpa ◦ float getGpa(); //returns the gpa 

 

•       A function display with the following prototype: 

◦ void display(); 
 

When executed, display should print the student’s first name, last name, id, and GPA on a single line separated by a single space, as follows: 

                John Doe 3921093129 3.5 
 

b) Separate the class declaration and implementation into Student.h and Student.cpp. You can use #ifndef / # define / #endif to prevent multiple class declarations. 

 

 

We provide a Q3main.cpp file to test your implementation. 

More products