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);
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
Variable |
Start address |
End address |
a |
||
pc |
||
c |
||
i |
||
pk |
||
k |
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 |
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;
}
output
2.
Learn the example C program in Folio consisting of three files (a.c, a.h, main.c) and complete this exercise. You need t...
#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 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 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 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 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 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 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 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 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 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...