Created
April 16, 2014 21:22
-
-
Save addie/7eba43a791329591007e to your computer and use it in GitHub Desktop.
Playing with c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Write a program Array, that accepts arguments from the command line (argc, *argv[ ])as follows: (20%) | |
➢ Array Type Size InitValue | |
o The above program will create an array on the heap of type (Type: float, int, chars, etc) having a size (Size). | |
o After creating the array on the heap, the user should initialize the array with the entry values having initial value of InitValue to InitValue + Size | |
o Print out the total number of array elements in a block type format (i.e. 8 values per line) | |
o Deallocates the memory from the heap | |
Demonstrate that the following Use Cases: | |
Array float 32 1 | |
Array char 32 65 | |
Array float 64 24 | |
Array int 96 32 15*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[] ) | |
{ | |
int var = 1; | |
char* type = argv[1]; | |
int size = 0; | |
int initvalue = 0; | |
int i = 0; | |
float* buffer1; | |
int* buffer2; | |
char* buffer3; | |
sscanf(argv[2], "%i", &size); | |
sscanf(argv[3], "%i", &initvalue); | |
printf("type: %s\n", type); | |
printf("size: %d\n", size); | |
printf("val: %d\n", initvalue); | |
if (strcmp(type, "float") == 0) | |
{ | |
buffer1 = (float*) malloc (size*sizeof(float)); | |
if (buffer1==NULL) exit (1); | |
for(i = 0; i < size; i++) | |
{ | |
buffer1[i]=(float)initvalue++; | |
printf("%d ", buffer1[i]); | |
} | |
for(i = 0; i < 8; i++) | |
{ | |
printf("%d ", buffer1[i]); | |
} | |
free(buffer1); | |
} | |
else if (strcmp(type, "int") == 0) | |
{ | |
buffer2 = (int*) malloc (size*sizeof(int)); | |
if (buffer2==NULL) exit (1); | |
for(i = 0; i < size; i++) | |
{ | |
buffer2[i]=(int)initvalue++; | |
} | |
free(buffer2); | |
} | |
else if (strcmp(type, "char") == 0) | |
{ | |
buffer3 = (char*) malloc (size*sizeof(char)); | |
if (buffer3==NULL) exit (1); | |
for(i = 0; i < size; i++) | |
{ | |
buffer3[i]=(char)initvalue++; | |
} | |
free(buffer3); | |
} | |
else | |
printf("Invalid Type\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment