Help! I need to write a program in C that takes 2 command line arguments that ar
ID: 3665574 • Letter: H
Question
Help! I need to write a program in C that takes 2 command line arguments that are numeric (height & width). The program needs to print a block of random text letters with the specified dimensions (width characters per line, height lines). The program has to use a subroutine that takes the 2 parameters, allocates a char buffer, fills it randomly as appropriate, and returns a pointer to that buffer (a char*). The main method should print this buffer and free the allocated memory. *note: I am on a Linux vm therefore i am using the bash gcc compiler with gedit. Also, this is C not C++. Help! I need to write a program in C that takes 2 command line arguments that are numeric (height & width). The program needs to print a block of random text letters with the specified dimensions (width characters per line, height lines). The program has to use a subroutine that takes the 2 parameters, allocates a char buffer, fills it randomly as appropriate, and returns a pointer to that buffer (a char*). The main method should print this buffer and free the allocated memory. *note: I am on a Linux vm therefore i am using the bash gcc compiler with gedit. Also, this is C not C++. Help! I need to write a program in C that takes 2 command line arguments that are numeric (height & width). The program needs to print a block of random text letters with the specified dimensions (width characters per line, height lines). The program has to use a subroutine that takes the 2 parameters, allocates a char buffer, fills it randomly as appropriate, and returns a pointer to that buffer (a char*). The main method should print this buffer and free the allocated memory. *note: I am on a Linux vm therefore i am using the bash gcc compiler with gedit. Also, this is C not C++.Explanation / Answer
// C program:
#include <stdio.h>
#include <stdlib.h>
char** GenerateRandomText(int height, int width);
int main(int argc, char *argv[])
{
int height;
int weight;
int i,j;
height = atoi(argv[1]);
weight=atoi(argv[2]);
/*receiving the buffer,which contains randomly generated characters*/
char **RandomText = (char **)malloc(sizeof(char *) * (height*weight));
RandomText=GenerateRandomText(height,weight);
//printing contents of buffer
printf(" Randomly generated contents: ");
for(i=0;i<height;i++)
{
printf(" %s",*(RandomText + i));
}
free(RandomText);
return 0;
}
char** GenerateRandomText(int height, int width)
{
int i,j;
//buffer to store randomly generated characters
char buffer[height][width];
for(i=0;i<height;i++)
{
for(i=0;i<height;i++)
{
//generating random charcters
char randomletter = 'A' + (rand() % 26);
//storing random chars in a buffer
buffer[i][j]=randomletter;
}
}
//returning buffer to main function
return buffer;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.