Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PASS WEEK 13 SOLUTIONS
- PROBLEM #1
- #include <stdio.h>
- #include <stdlib.h>
- int main(){
- int* ptr = NULL;
- int numElem;
- printf("Enter in number of elements: ");
- scanf("%d", &numElem);
- ptr = (int*)calloc(numElem, sizeof(int));
- for(int i = 0; i < numElem; i++)
- {
- scanf("%d", &ptr[i]);
- }
- for(int i = 0; i < numElem; i++)
- {
- printf("%d ", ptr[i]);
- }
- free(ptr);
- return 0;
- }
- PROBLEM #2
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int* createArray(int);
- void fillArray(int, int, int*);
- void printArray(int, int*);
- int main (int argc, char* argv[]) {
- int sz = atoi(argv[1]);
- int value = atoi(argv[2]);
- int* array = createArray(sz);
- fillArray(value, sz, array);
- printArray(sz, array);
- return(0);
- }
- int* createArray(int size){
- int* ary = (int*) calloc(size, sizeof(int));
- return ary;
- }
- void fillArray(int value, int size, int* ary){
- int startValue = value;
- for (int i = 0; i < size; i++){
- ary[i] = value;
- value += startValue;
- }
- return;
- }
- void printArray(int size, int* ary){
- for (int i = 0; i < size; i++){
- printf(" %d ", ary[i]);
- }
- printf("\n");
- return;
- }
- PROBLEM #3
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main () {
- char *str;
- /* Initial memory allocation */
- str = (char *) malloc(15);
- strcpy(str, "wordswordswords");
- printf("String = %s, Address = %s\n", str, str);
- /* Reallocating memory */
- str = (char *) realloc(str, 25);
- strcat(str, “, morewords");
- printf("String = %s, Address = %s\n", str, str);
- free(str);
- return(0);
- }
- PROBLEM #4
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- void PrintArray(int, char**);
- int main(void) {
- char* ary[5];
- char tmp[80];
- int strSz = 0;
- int i =0;
- printf("Enter in a series of 5 strings:");
- while(i < 5){
- fscanf(stdin, "%s", tmp);
- ary[i] = (char*) malloc(sizeof(tmp));
- strcpy(ary[i++],tmp);
- }
- PrintArray(i, ary);
- return 0;
- }
- void PrintArray(int sz, char** aryIn){
- int i = 0;
- for(i = 0; i < 5; i++){
- printf("The value is %s\n", aryIn[i]);
- }
- return;
- }
- PROBLEM #5
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int GetDimensions(int * , int * , char * );
- int main(void) {
- char fName[80];
- int rows = 0;
- int cols = 0;
- printf("Enter the file name: ");
- scanf(" %s", fName);
- if (GetDimensions( & rows, & cols, fName)) {
- printf("The file has %d records and the largest is %d characters in length\n",
- rows, cols);
- } else {
- printf("The file could not be processed.\n");
- }
- return 0;
- }
- int GetDimensions(int * rows, int * max, char * fn) {
- FILE * fp = fopen(fn, "r");
- int cnt = 0;
- int tmp = 0;
- char buffer[1024];
- * max = 0;
- if (fp == NULL)
- return 0;
- else {
- while (fgets(buffer, 1024, fp) != NULL) {
- cnt++;
- tmp = strlen(buffer);
- if (tmp > * max) {
- * max = tmp;
- }
- }
- * rows = cnt;
- fclose(fp);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement