Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C Language 1. Pass by Value vs. Pass by reference. A. Pass by Value B. Pass by reference 2. Array (1,2,3...D arrays) A. Find

In C Language

1. Pass by Value vs. Pass by reference.

A. Pass by Value

B. Pass by reference

2. Array (1,2,3...D arrays)

A. Find the sum of a single column in a 2D array.

3. Dynamic memory allocation

A. Allocate enough memory to store a single string (don't forget to null terminate!).

4. Struct

A. Allocate memory for a single struct and return a pointer to said struct.

B. Free any associated memory with the node, including any variables inside the node.

5. Command line arguments

A. Save a list of strings storing all of the individual command line arguments (allocate exact memory for each string) and list.

6. Linked list

A. Add a node to the front (push function).

B. Remove a node from the front (pop function).

In the lab10 directory include the following files,

passBy.c

array.c

dynamicMemory.c

commandLineArgs.c

struct.c

linkedList.c

---------------------------------------------------------------------------------------------------

1. Setup Environment

%%file lab10/lab10.h

/* This ifndef will stop the pesky "previous definition"/"redefinition" error when the preprocessor reads the file multiple times */ #ifndef LAB_10

/* Define this library */ #define LAB_10

/***** * Standard Libraries *****/

#include #include #include

/***** * Struct Definitions *****/

/* enum and typedef of a boolean type */ typedef int boolean; enum boolean{false, true};

/* Linked list node and typedef */ typedef struct listNode Node; struct listNode { char *data; Node *next; };

/***** * Function Prototypes *****/

/*** 1. Passing parameters ***/

/* Return the value of num squared */ int squared_value(int num);

/* Set the value of &num to be num squared */ void squared_reference(int *num);

/*** 2. Arrays ***/

/* Calculate the sum of a single column in an array with 8 rows and 7 columns */ int sumColumn(int array[8][7], int column);

/*** 3. Dynamic Memory ***/

/* Allocate enough memory, and copy the */ char *copyString(const char *toCopy);

/*** 4. Command Line Arguments ***/

/* Allocate the correct amount of memory for storing the string value of all strings passed */ void storeCommandLineArgs(int argc, const char *argv[], char ***args);

/*** 5. Structs ***/

/* Allocate memory for a single Node struct and return a pointer to said struct */ Node *newNode(char *string);

/* Free any associated memory with a Node, including the node itself */ void deleteNode(Node **toDelete);

/*** 6. Linked Lists ***/

/* Add a node to the front (push function). You can assume the node is already allocated */ void addToFront(Node **head, Node *toAdd);

/* Remove a node from the front (pop function). Return the node that is removed from the list. */ Node *removeFromFront(Node **head);

#endif

----------------------------------------------------------------------------------------------------------

%%file lab10/main.c

#include "lab10.h"

int main(int argc, char *argv[]) { printf("Hello, World "); return 0; }

----------------------------------------------------------------------------------------------------------

2. Testing File

The following file is used for testing so any code you write will automatically return your score. There is no need for you to change anything in this file, it's just included so you can create the testing files locally. If you would like to use the testing files, don't forget to include them when you compile.

%%file lab10/testing_10.h

#include "lab10.h"

#ifndef TESTING_10

#define TESTING_10

/* Return the highest integer value */ int max(int one, int two);

/* Number 1: Passing Parameters */ int testOne(char *result[]);

/* Number 2: Arrays */ int testTwo(char *result[]);

/* Number 3. Dynamic memory allocation */ int testThree(char *result[]);

/* Number 4. Command line Arguments */ int testFour(char *result[], int argc, const char *argv[]);

/* Number 5. Structs */ int testFive(char *result[]);

/* Number 6. Linked list tests */ int testSix(char *result[]);

#endif

--------------------------------------------------------------------------------------------

%%file lab10/testing_10.c

#include "lab10.h" #include "testing_10.h"

int max(int one, int two) { if (one > two) { return one; } else { return two; } }

/* Number 1: Passing Parameters */ int testOne(char *result[]) { int passByValue = 0; int passByReference = 4; int score = 0; printf("*** 1. Testing pass by value vs. pass by reference "); passByValue = squared_value(4); squared_reference(&passByReference); score += (passByValue == 16); score += (passByReference == 16); printf("\t pass by value result = %s ", result[passByValue == 16]); printf("\t pass by reference result = %s ", result[16 == passByReference]); printf("\t\t score = %d of 2 ", score); return score; }

/* Number 2: Arrays */ int testTwo(char *result[]) { int array[8][7]; int x, y; int score = 2; int sums[] = {36, 72, 108, 144, 180, 216, 252}; boolean correctSum = true; /* Initialize array */ for (y = 0; y < 8; y++) { for (x = 0; x < 7; x++) { array[y][x] = (x+1)*(y+1); } } printf("*** 2. Arrays "); for (x = 0; x < 7; x++) { score -= (sumColumn(array, x) != sums[x]); if (sumColumn(array, x) != sums[x]) { correctSum = false; printf("\t FAILED: %d != %d ", sumColumn(array, x), sums[x]); } } score = max(score, 0); printf("\t\t sum column result: %s ", result[correctSum]); printf("\t\t score = %d of 2 ", score); return score; }

/* Number 3. Dynamic memory allocation */ int testThree(char *result[]) { const char stringTest[] = "asdfg 98765 abc123"; char *copyTest = NULL; int score; printf("*** 3. Dynamic memory allocation "); copyTest = copyString(stringTest); printf("\t\t CopyString result = %s ", result[strcmp(stringTest, copyTest) == 0]); score = 2*(strcmp(stringTest, copyTest) == 0); printf("\t\t score = %d of 2 ", score); free(copyTest); return score; }

/* Number 4. Command line Arguments */ int testFour(char *result[], int argc, const char *argv[]) { int y, score = 4; char **commandArgs = NULL; boolean argsCopied = true; printf("*** 4. CommandLineArguments "); storeCommandLineArgs(argc, argv, &commandArgs); for (y = 0; y < argc; y++) { if (strcmp(argv[y], commandArgs[y]) != 0) { argsCopied = false; printf("\t FAILED: %s != %s ", argv[y], commandArgs[y]); score -= 1; } } /* Get score and free memory */ score = max(score, 0); for (y = 0; y < argc; y++) { free(commandArgs[y]); } free(commandArgs); printf("\t\t command line arguments copied = %s ", result[argsCopied]); printf("\t\t score = %d of 4 ", score); return score; }

/* Number 5. Structs */ int testFive(char *result[]) { boolean newNodeTest = true; boolean deleteNodeTest = true; Node *list[5]; char *strings[] = {" First String", "second", "stringthree", "String 4", "LaSt"}; int i; int score, delScore = 2, newScore = 2; printf("*** 5. Structs "); for (i = 0; i < 5; i++) { /*list[i] = newNode(strings[i]);*/ list[i] = newNode(strings[i]); if (strcmp(strings[i], list[i]->data) != 0 || list[i]->next != NULL) { printf("\t FAILED: %s != %s ", strings[i], list[i]->data); if (list[i]->next != NULL) { printf("\t - Don't forget about NULL... "); } newNodeTest = false; newScore -= 1; } } for (i = 0; i < 5; i++) { deleteNode(&list[i]); if (list[i] != NULL) { printf("\t FAILED delete: for node %d: %s ", i, strings[i]); printf("\t - Make sure you set the pointers to NULL after it has be freed. "); deleteNodeTest = false; delScore -= 1; } } score = max(newScore, 0) + max(delScore, 0); printf("\t\t struct allocate result = %s ", result[newNodeTest]); printf("\t\t linked list remove result = %s ", result[deleteNodeTest]); printf("\t\t score = %d of 4 ", score); return score; }

/* Number 6. Linked list tests */ int testSix(char *result[]) { Node *head = NULL; boolean addToFrontTest = true; boolean removeFromFrontTest = true; int i, score, pushScore = 2, popScore = 2; char *strings[] = {"one", "two", "three", "head"}; printf("*** 6. Linked list "); for (i = 0; i < 4; i++) { Node *new = newNode(strings[i]); addToFront(&head, new); if (strcmp(strings[i], head->data) != 0) { printf("\t FAILED add: %s != %s ", strings[i], head->data); addToFrontTest = false; pushScore -= 1; } } i = 3; while (head != NULL) { Node *removed = removeFromFront(&head); if (strcmp(strings[i], removed->data) != 0) { printf("\t FAILED remove: %s != %s ", strings[i], removed->data); removeFromFrontTest = false; popScore -= 1; } /* ***NEED TO FREE MEMORY HERE*** */ deleteNode(&removed); i--; } score = max(pushScore, 0) + max(popScore, 0); printf("\t\t linked list add result = %s ", result[addToFrontTest]); printf("\t\t linked list remove result = %s ", result[removeFromFrontTest]); printf("\t\t score = %d of 4 ", score); return score; }

-----------------------------------------------------------------------------------------------------

3. Compiling Flags

The standard flags for compiling during this lab are below.

Wall

pedantic

ansi

4. Review files

Pass by value = creating a copy of the data passed

Pass by reference = passing a reference to the data (pointer to data)

Note: There is a way to code a swap function for all pointers using void pointers, but this lab will typecast the data.

%%file lab10/passBy.c

#include "lab10.h"

/* Return the value of num squared */ int squared_value(int num) { return 0; }

/* Set the value of &num to be num squared */ void squared_reference(int *num) {

}

===============================================

%%file lab10/array.c

#include "lab10.h"

/* Calculate the sum of a single column in an array. 8 rows and 7 columns */ int sumColumn(int array[8][7], int column) { return 0; }

====================================================

%%file lab10/dynamicMemory.c

#include "lab10.h"

/* Allocate enough memory, and copy the */ char *copyString(const char *toCopy) { return NULL; }

=====================================================

%%file lab10/commandLineArgs.c

#include "lab10.h"

void storeCommandLineArgs(int argc, const char *argv[], char ***args) { /* Hint: you have already written a copy string function */ }

=================================================

%%file lab10/struct.c

#include "lab10.h"

/* Allocate memory for a single Node struct and return a pointer to said struct */ Node *newNode(char *string) { /* Hint: You should allocate memory for the new node, and the pointer in the node */ return NULL; }

/* Free any associated memory with a Node, including the node itself */ void deleteNode(Node **toDelete) { /* Hint: Make sure you set the node you free to NULL!*/ }

====================================================

%%file lab10/linkedList.c

#include "lab10.h"

/* Add a node to the front (push function). */ void addToFront(Node **head, Node *toAdd) { /* You can assume that the node toAdd has already been allocated and is not empty */ }

/* Remove a node from the front (pop function). Return the node that is removed from the list. */ Node *removeFromFront(Node **head) { /* Hint: remember to check if the linked list is empty. */ return NULL; }

========================================================

%%file lab10/main.c

/* Lab header file */ #include "lab10.h"

/* Testing prototypes */ #include "testing_10.h"

/* Here is the main function for testing */ int main(int argc, const char *argv[]) { char *result[] = {"FAIL", "PASS"}; int score = 0; printf("Lab 10: CIS*2500 Review "); printf("\t - Many hardcoded unit tests are included for your use in this lab, no need for you to change main. "); printf("\t - Any failed test cases will be printed for you "); /*** Uncomment the tests when you are ready to begin testing ***/ score += testOne(result); score += testTwo(result); /*score += testThree(result); score += testFour(result, argc, argv); score += testFive(result); score += testSix(result);*/ printf("Your score = %d/18 ", score); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

JDBC Database Programming With J2ee

Authors: Art Taylor

1st Edition

0130453234, 978-0130453235

More Books

Students also viewed these Databases questions

Question

Define Administration?

Answered: 1 week ago