C Exam Questions#
2021 AA#
1.a(15) Write a program that gets a number of command line’s arguments from the user, the program has to print the number of arguments (excluding the name of the program) that as a string they are 1 char long.
2.b(15)
Given the below linked list:
typedef struct list_t {
int val;
struct list_t* next;
} LIST;
Write a function concat the receives 2 pointers to non empty linked lists, and concatenates the second LL to the end of the first one, no need to return anything.
3.c(15)
Write a function called create, that gets a natural number n and returns a pointer called a, the pointer should point to a data structure that stores n*n integers, and it’s elements are accecible using this syntaxt a[i][j] (i,j are between 1-n) and all values are initialized to 0.
Solution
void main(int argc, char* argv[])
{
int count = 0;
for (int i = 1; i < argc; ++i)
{
char* arg = argv[i];
if(arg[1] == '\0')
{
count++;
}
}
printf("%d\n", cou);
}
void concat(LIST* a, LIST* b)
{
while (a->next != NULL)
{
a = a->next;
}
a->next = b;
}
int** create(int n)
{
int i, j;
int **vals = (int**)malloc(n * sizeof(int*));
assert(vals);
for (i = 0; i < n; i++)
{
int* m = (int*)malloc(n * sizeof(int));
assert(m);
for (j = 0; j < n; j++)
{
m[j] = 0;
}
vals[i] = m-1 ;
}
return vals-1;
}