Question

Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...

Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need to understand function, pointer and the selection sort algorithm. Write a C program that consists of three files mysort.h, mysort.c and myMain.c. Below is the mysort.h prototype

#include <stdlib.h>

#include <stdio.h>

void generateNums(int *myarr, int len);

void sortNums(int *myarr, int len);

  • mysort.h must contain only the function declarations (prototypes) listed above
  • generateNums function should generate len random integers in the range 0~99 inclusive (use rand function in <stdlib.h>) in the supplied array myarr. Its definition should be in mysort.c.
  • sortNums function should sort the len integers at myarr into ascending order using selection sort algorithm. Its definition should be in mysort.c. Note that the caller of sortNums and generateNums should pass valid pointers myarr to them.
  1. In myMain.c you need to write your main function that calls the two functions in mysort.h to generate N random integers in the range 0~99 inclusive and then print out the generated unsorted random numbers in one line with suitable separators between them and the sorted N numbers in another line. Your program should get N (which represents the number of random integers you will generate and sort) from the first command line argument (i.e. argv[1]) of this C program (you can use the atoi function; see example code LeapYear_cmdline.c). Submit all three source files. (37 pts)
  2. Submit a makefile to compile your program. Be aware of the possible file name issue regarding Folio complaint. See the last slide of 3_Pointers_Functions.pptx for details. Make sure you have tested that your makefile works. (3 pts)

2. (40 pts) You need to understand array, pointer and pointer arithmetic to complete this exercise. And you should be able to figure out the answers without actually compiling and running the program. You may recall problem 4 of assignment 2.

#include <stdio.h>

int main(int argc, char *argv[])

{

     char a, *pc, c[9];

     int i, *pk, k[9];

     a='z';

     pc=&(c[8]);

     pk=&(k[0]);

     for (i=0; i<9; i++)

    {

          *pc=a-(char)i;

          pc--;

          *pk=(int)a-i;

          pk++;

         }

                  return 0;

}//end of main

  1. Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a is 100. Suppose sizeof(char)=1, sizeof(int)=4 and the size of a memory address (a pointer) is 8 bytes. You may assume there is no gap in memory between the variables. (18 pts)

Variable

Start address

End address

a

pc

c

i

pk

k

  1. Additionally, show values of the variables in the following table at the end of execution of the for loop before the main function returns. (For the two array variables c and k, list the contents of all array elements.) (18 pts)

Variable

Value

a

pc

Use an expression involving c to represent pc’s value

c

list the contents of the entire array

i

pk

Use an expression involving k to represent pk’s value

k

list the contents of the entire array

  1. True or false? pc and pk have the same value just before return. (2 pts)
  2. True or false? The two array c and k contain the same integral values just before return. (2 pts)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program :

File mysort.h contents:

#include<stdio.h> // header file declarations
#include<stdlib.h>
void generateNums(int *myarr, int len); // function prototype declarations
void sortNums(int *myarr, int len);

File mysort.c contents:

#include<stdio.h> // header file declarations

#include<stdlib.h>

#include<time.h>

#include "mysort.h" // importing the mysort function prototype file

void generateNums(int *myarr, int len) {

srand(time(NULL));

for (int i = 0; i < len; i++)

myarr[i] = rand() % (100 + 1 - 0) + 0; // generating random numbers from 0 to N using rand function

}

void sortNums(int *myarr, int len) {

int pos, j, swap;

for (int i = 0; i < len - 1; i++) {

pos = i;

for (j = i + 1; j < len; j++) {

if (myarr[pos] > myarr[j])

pos = j; // applying the selection sort algorithm

}

if (pos != i) {

swap = myarr[i]; // swapping the two integers for correct sorting

myarr[i] = myarr[pos];

myarr[pos] = swap;

}

}

}

File myMain.c contents:

#include<stdio.h> // header file declarations

#include<stdlib.h>

#include "mysort.h" // importing the mysort function prototype and declaration files

#include "mysort.c"

int main(int argc, char **argv) {

int N = atoi(argv[1]); // taking the number of random numbers to be generated as command line argument

int myarr[N], i;

generateNums(myarr, N); // generating random number from 0 to N

printf("\nThe numbers before sorting are: ");

for (i = 0; i < N; i++)

printf("%d ", myarr[i]); // printing the unsorted random numbers in one line

printf("\n");

sortNums(myarr, N); // sorting the unsorted list of numbers

printf("\nThe numbers after sorting are: ");

for (i = 0; i < N; i++)

printf("%d ", myarr[i]); // printing the sorted random numbers in next line

printf("\n");

return 0;

}

E a (0:22, 96%) С mysort.h )-gedit 40 Tue May 29 2018 07:27:42 save mysort.h myMain.c mysort.c #include«stdio.h> #include«stdE R A mysort.c gedit (0:18, 96%) CE q) Tue May 29 201 8 07:33:14 Open Save #include«stdio.h> #include«stdlib.h> #include« tinE R A myMain.c ) -gedit (0:22, 96%) CE q) Tue May 29 201807:27:47 Open ▼ Save mysort.h myMain.c mysort.c #include«stdio.h> #ioutput

ㄧ 宋小 . sourav@sourav-HP-Pavilion: sourav@sourav-HP-Pavilion$ vim mysort.h sourav@sourav-HP-Pavilion- vim mysort.c sourav@sour2.

Varlable Start Address End Address 100 100 101 109 pc 110 118 119 136 169 Values of the variables Varlable Value cli] = a-Cha

Add a comment
Know the answer?
Add Answer to:
Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='...

    #include <stdio.h> int main(int argc, char *argv[]) {      char a, *pc, c[9];      int i, *pk, k[9];      a='z';      pc=&(c[8]);      pk=&(k[0]);      for (i=0; i<9; i++)     {           *pc=a-(char)i;           pc--;           *pk=(int)a-i;           pk++;           }                     return 0; }//end of main Answer the below questions with the above code Write out the memory map for the above C code in the following table. For array variables, list the address range for the entire array. Assume the memory address starts from 100, that is, the address for a...

  • 1. (40 pts) Note: All programs MUST run in Ubuntu. Following the example C program in...

    1. (40 pts) Note: All programs MUST run in Ubuntu. Following the example C program in Folio consisting of three files (a.c, a.h, main.c), write a C program that consists of three files mysquare.h, mysquare.c andmyMain.c. Below is the mysquare.h prototype #include <stdlib.h> #include <stdio.h> void generateNums(int *myarr, int len); void squareNums(int *myarr, int len); void printNums(int *myarr, int len); mysquare.h must contain only the function declarations (prototypes) listed above generateNums function should generate len random integers in the range...

  • Need C programming help. I've started to work on the program, however I struggle when using...

    Need C programming help. I've started to work on the program, however I struggle when using files and pointers. Any help is appreciated as I am having a hard time comleting this code. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_LINE 100 #define MAX_NAME 30 int countLinesInFile(FILE* fPtr); int findPlayerByName(char** names, char* target, int size); int findMVP(int* goals, int* assists, int size); void printPlayers(int* goals, int* assists, char** names, int size); void allocateMemory(int** goals, int** assists, char*** names, int size);...

  • State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator...

    State true or false: (6 x 2 = 12 pts) Using -- with a reverse iterator moves it from the back toward the front of a list b) The body of a do-while loop is always executed at least once. c) If p points to the array element a[j], then p + i points to a[i+j]. d) Any number of pointer variables may point to the same object. In terms of both execution speed and memory usage usually iterative solutions...

  • Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You...

    Homework Part 1 Write a C program that declares and initializes a double, an int, and a char. You can initialize the variables to any legal value of your choosing. Next, declare and initialize a pointer variable to each of the variables Using printf), output the following: The address of each of the first three variables. Use the "Ox%x" format specifier to print the addresses in hexadecimal notation The value of each variable. They should equal the value you gave...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • Run the C program below and complete the table showing all the variables. Add printf function...

    Run the C program below and complete the table showing all the variables. Add printf function calls to obtain the addresses and values of all 13 variables. (Count the array (ca) as 3 variable/values and also include argc and argv in the table). The table must show the Address, Name, Datatype, Scope, and Value of each variable on the stack. (Hint: use the sizeof function to double check your addresses.) Explain how different the actual memory allocations are from what...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • write program in C language. To get more practice working with files, you will write several...

    write program in C language. To get more practice working with files, you will write several functions that involve operations on files. In particular, implement the following functions 1. Write a function that, given a file path/name as a string opens the file and returns its entire contents as a single string. Any endline characters should be preserved char *getFileContents (const char filePath); 2. Write a function that, given a file path/name as a string opens the file and returns...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT