Great Deal! Get Instant \$10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

# 25/07/2020 COMP1911 Week 08 Laboratory Exercises https://cgi.cse.unsw.edu.au/~cs1911/20T2/lab/08/questions 1/3 COMP1911 20T2 (https://webcms3.cse.unsw.edu.au/COMP1911/19T1) Introduction to Programming...

25/07/2020 COMP1911 Week 08 Laboratory Exercises
https:
cgi.cse.unsw.edu.au/~cs1911/20T2/la
08/questions 1/3
COMP1911 20T2 (https:
webcms3.cse.unsw.edu.au/COMP1911/19T1)
Introduction to Programming (https:
webcms3.cse.unsw.edu.au/COMP1911/19T1)
Objectives
In this Lab, you will practise:
malloc
strings
argc, argv
2D a
ays
Preparation
Before the lab you should re-read the relevant lecture slides and their accompanying examples
Getting Started
Login and run following commands inside a Unix terminal
Create a new directory for this lab called lab08 by typing:
mkdir lab08
Change to this directory by typing:
cd lab08
Introduction
This week we will be reinforcing your understanding of functions, a
ays, and strings.
Each activity covers one of these points, however it requires the use of malloc and free.
Remember that the standard pattern for malloc is (if I'm mallocing an "int")

This is a stack variable, this is what we'd do without using malloc
int a = 7;
printf("a: %d\n", a);

But we're going to use malloc now instead!

This is how to create and initialise a heap variable
int *b = (int *)malloc(sizeof(int));
if (b == NULL) {
printf("malloc e
or\n");
exit(1);
}
*b = 7;

Then we'll use our heap variable
printf("b: %d\n", *b);

Then when we're done we'll free it, and set it's value to NULL
free(b);
= NULL;

Exercise 1: Mallocing Memory
Modify the C program we've written mallocMem.c which uses malloc to create memory on the heap for variables.
You can copy this file into your cu
ent directoy by running
Week 08 Laboratory
Exercises
https:
webcms3.cse.unsw.edu.au/COMP1911/19T1
https:
webcms3.cse.unsw.edu.au/COMP1911/19T1
25/07/2020 COMP1911 Week 08 Laboratory Exercises
https:
cgi.cse.unsw.edu.au/~cs1911/20T2/la
08/questions 2/3
\$ cp ~cs1911/public_html/tl
08/mallocMem.c .
\$ dcc mallocMem.c -o mallocMem
heapInteger: 3
heapDouble: 4.0
If malloc fails at any point (due to not being able to allocate memory), please print the following e
or:
malloc e
o
Once you get the program functioning, make sure you have co
ectly used "free" function to free the memory, and then set the
pointers to be equal to NULL (it's good practice).
\$ 1911 autotest lab08 mallocMem.c
Exercise 2: Malloc and Strcpy
Modify the C program we've written strcpy.c which takes in a single command line argument, creates a copy of it using malloc,
and then prints both.
To create a copy of a string, you first need to malloc space for the new character a
ay (don't forget to add an extra "+ 1" for the
'\0' terminator!).
Once you have malloc'd this you then need to use strcpy
(https:
www.tutorialspoint.com/c_standard_li
ary/c_function_strcpy.htm) to copy data from the old string into the new string.
You can copy this file into your cu
ent directoy by running
\$ cp ~cs1911/public_html/tl
08/strcpy.c .
If no arguments are passed in, or more than 1 argument is passed in, then the program should execute the following code:
exit 0;
Expected output includes:
\$ ./strcpy "Hello there"
String 1: Hello there
String 2: Hello there
\$ ./strcpy "Hello there" "Second argument"
If malloc fails at any point (due to not being able to allocate memory), please print the following e
or:
malloc e
o
Once you get the program functioning, make sure you have co
ectly used "free" function to free the memory, and then set the
pointers to be equal to NULL (it's good practice).
\$ 1911 autotest lab08 strcpy.c
Exercise 3: Building an A
ay
Modify the program we've written a
ayBuild.c which takes a command line argument (a set of numbers 0-9) and populate a 3 x 3
a
ay with those numbers.
You can copy this file into your cu
ent directoy by running
\$ cp ~cs1911/public_html/tl
08/a
ayBuild.c .
E.G. Passing in " XXXXXXXXXX" is passing in 1, 2, 3, 4, 5, 6, 7, 8, 9. Then we fill our a
ay the same way we write in English. Across
each row, top to bottom. So an input argument of " XXXXXXXXXX" would result in a 2D a
ay that could be visualised like
https:
www.tutorialspoint.com/c_standard_li
ary/c_function_strcpy.htm
25/07/2020 COMP1911 Week 08 Laboratory Exercises
https:
cgi.cse.unsw.edu.au/~cs1911/20T2/la
08/questions 3/3
1 2 3
4 5 6
7 8 9
This is a very efficient way to populate an a
ay via command line.
Similar to previous programs, things may go wrong, and the appropriate e
or must be printed.
If the user doesn't input the right number of arguments, print:
ay
If the user enters the right argument, but it's the wrong size (not 3 x 3 worth of elements) print:
ect size
If malloc fails at any point (due to not being able to allocate memory), please print the following e
or:
malloc e
o
In this exercise the 2D a
ay is represented by a type int** which is the same as int[][] .
\$ 1911 autotest lab08 a
ayBuild.c
Submission/Assessment
When you are satisfied with your work, ask your tutor to assess it. You are to submit it electronically by typing (run this command
give cs1911 lab08 mallocMem.c strcpy.c a
ayBuild.c
Remember the lab assessment guidelines (../../lab_assessment.html) - if you don't finish the exercises you can finish them in you
own time, submit them by 19:59:59 Sunday using give and ask ask tutor to assess them at the start of the following lab.
You can also just run the autotests without submitting by typing
1911 autotest lab08
https:
cgi.cse.unsw.edu.au/~cs1911/20T2/lab_assessment.html
Answered Same Day Aug 01, 2021

## Solution

Arun Shankar answered on Aug 03 2021
#include #include #include #define SIZE 3
void printA
ay(int **a
);
int main(int argc, char *argv[]) {
if(argc!=2)
{
ay\n");
return 0;
}
if(strlen(argv[1])!=9)
{
ect size\n");
return 0;
}
...
SOLUTION.PDF

More Questions Â»

### Submit New Assignment

Copy and Paste Your Assignment Here