CPSC1021 Lab 2-Fun with Pointers Solved

35.00 $

Category:

Description

Rate this product

Lab Objective

  • Build familiarity with double pointers ● Understand how strings work in C.
  • Practice passing data by reference
  • Working with multiple files

Introduction

The purpose of this lab is to continue to build familiarity with pointers. In this lab, we will use a double pointer to hold a first and last name. You will then print how long the first and last name is. Lastly, you will print the first and last name in reverse.  

 

You are required to implement the functions as they are given. While you are working through the various steps, you are free to comment out the functions you have not implemented, so you can compile and test your program as you work your way through the lab.            

Step 1: Allocate Space for 2D Pointer

To begin this lab, you need to malloc space for a 2D pointer. The size of your 2D pointer will 2 X 100. Each row of columns will hold a name. The first index of the pointer will hold the first name and the second will hold the last name. Once you have properly allocated space for the pointer, ask the user for input and store the input into the arrays.

Match this format:             

Step 2: Implement the SizeOfName() function. 

This function should parse through the text and grab the length of the first name and the last name. You should recall that scanf will add a null character at the end of a captured string.

When you have calculated the length of a string, pass these variables by reference to printSizeOfName() function. Inside this function, you should print how long their first and name is. Match the following output:

Final Step: Implement reverseString() Function

Now that you have the functions above implemented and working, your last step is to implement the final function to print the string in reverse order. Here is an example of what the final program should look like.

Other Requirements:

In this lab you will be introduced to the use of multi-file programs. We have provided you with main.c. Using the information from this file you will produce 3 files, described below:

  1. c – This is the driver file. The “main” function will be implemented in this file.
  2. h – You will need to move the function prototypes to this file. You are also required to use header guards in this file. Below I have provided a information about header guards and why you need them.
  3. c – You will implement the functions in this file.

Multiple Files and Header Guards

At this point you should have already encountered multi-file programs. For those of you that have not we will give you a quick explanation of multiple files and header guards.

The early years of computer programming, where programs were relatively small compared to programs today, is long gone. Writing all code in the driver file does not exhibit good programming practice. It is important that you get experience developing multi-file programs.

Rather than putting your function prototypes in the driver file above the main, you will need to put the function protypes in the functions.h file. So, where do you think the function implementation should go? If you are thinking functions.c, you would be correct. Now the next question you should be considering, is how does the driver program (main function) or “Mr. Compiler” know where the function prototypes live. Well we are going to tell them using a #include statement. The syntax of this is as follows:

#include “functions.h”

This statement should be put in the main and any “.c” file that will use the functions listed in the file.

Since we are providing the implementation of our functions in the functions.c file we need to also include the functions.h in the functions.c file.

While working with multi-file programs has many advantages, there is one issue that needs to be addressed. It is not uncommon that a file could be included in another file multiple times. As an example, suppose we include the following in main.c:

#include <stdio.h>

#include “functions.h”

#include “anotherFunctions.h”

Now suppose anotherFunctions.h includes functions.h

By including functions.h and anotherFunctions.h in your main you have inadvertently included functions.h twice. Which could cause a nasty compile error. This can be avoided by using header guards in your “.h” files.

From the following website:

https://www.learncpp.com/cpp-tutorial/header-guards/

Header guards are conditional compilation directives that take the following form:

#ifndef  SOME_UNIQUE_NAME_HERE

#define SOME_UNIQUE_NAME_HEAR

// YOUR PROTOTYPES GO HERE

#endif

#ifndef stands for if not defined then you should define this file as …. #endif means this is the end of the if statement

You should use these header guards in all of your “.h” files.

FORMATTING You will need to add a header to each of your files like the following:

/**************************

*Your name

*CPSC 1021, your Lab Section, F19

*Your email

*Course Instructor

**************************/

Your program should compile with no warnings and no errors. If you program will not compile you will receive a 0 on the lab.

  • Your code should be well documented. (comments) There should be no lines of code longer than 80 characters.
  • You should use proper and consistent indention.

Any infractions of the above listed requirements will result in a 5 point deduction for each infraction category.

 

Turning in Your Assignment

Handin

You will use the School of Computing handin system to submit your lab files.

 

Prior to handing in your assignment make sure you test your program on the SOC servers. We will grade your assignment on the SOC server and will not accept the excuse that “it ran on my computer”. Your lab must run on the SOC servers.

 

Most labs will require you to use the tar utility.  The term “tar” stands for tape archive, an archiving file format. You will use the tar utility to “tar zip” (tar.gz) all files prior to submitting through handin. Make sure you are inside the current lab2 directory and files are inside the same directory.  Using the terminal, tar your file as follows:

 

tar –czvf <username>_lab2.tar.gz *

The <username> is your username.

 

Let’s break this down:

tar – the tar utility command -czvf are flags used by the tar utility

c – create a new archive containing the speciied items         z – (c mode only) Compress the resulting archive with gzip      v – verbose output (tar will list each file name as it is read from or written to the archive)            f – read the archive from or write the archive to the specified file username_lab2.tar.gz  – this is the file name. This must come before the list of names of the files to tar. *- this is the wildcard. This will archive all files in the current directory.  If you do not use the wildcard, you must type the name of each file you want archived.

 

Once you have tarred your file you should always check the files in your tarred file to determine if they are all there and in workable order.  The command to untar a file is as follow.

 

tar –zxvf <username>_lab2.tar.gz

 

Useful Linux commands

 

Command Purpose
ls  list all contents in your current directory
cd  takes you back to your home directory
cd .. takes you back one directory
cd dir_path takes you to the directory specified by the path provided
mv src_file dest_file renames src_file to dest_file
mv src_file dest_path moves src_file to the directory specified by the dest_path
mv src_file dest_dir/dest_file moves src_file to the directory specified by dest_path and gives it the name dest_file
cp src_file dest_file copies src_file to dest_file
cp src_file dest_path copies src_file to the directory specified by dest_path
cp src_file dest_dir/dest_file copies src_file to the directory specified by dest_path and gives it the name dest_file
rm file_name deletes the file named file_name
mkdir dir_name creates a directory name dir_name in your current directory
rmdir dir_name deletes the directory named dir_name if it is empty
rm -rf dir_name deletes the directory named dir_name (be careful when using –rf)
cat src_file shows the contents of the file on the screen without opening it in an editor
control-c send the terminate signal to a running process (kills the current process); good to use if your program is stuck in an infinite loop, for example
ps shows a listing of processes that are running
kill -9 [pid] kills the process you specify with the pid (process id #) which is shown when you type ps
man unix_command displays the manual page (help page) for the specified Unix command
logout logs you out

 

  • Lab2-toqq02.zip