Create a directory called exam3 using mkdir exam3 and move into that directory w
ID: 3811131 • Letter: C
Question
Create a directory called exam3 using mkdir exam3 and move into that directory with cd exam3 to complete the two programs shown below. The skeleton code for each problem (shown below) can be downloaded from the course Blackboard site. You are not allowed to use the Internet while coding. You can log into the cs-intro server to test your programs. Name this program one.c This program reads in a matrix of integers. It then calls hasDups to see if there are any duplicates in the matrix (a given value occurs more than once). You must write hasDups. #include #include int **allocateArray(int r, int c) { int **array = (int **) malloc (sizeof (int *)*r); for (int a=0; aExplanation / Answer
//one.c
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
//To allocate memory
int **allocateArray(int r, int c)
{
int a;
//Allocate memory for rows
int **array = (int **)malloc(sizeof(int *) * r);
//Allocate memory for columns
for(a = 0; a < r; a++)
{
array[a] = (int *)malloc(sizeof(int) * c);
}
//Returns the matrix
return array;
}//End of function
//To accept data and store it in matrix
void readArray(int **array, int r, int c)
{
int a, b;
//Loops till r value
for(a = 0; a < r; a++)
{
//Loops till c value
for(b = 0; b < c; b++)
//Accepts data
scanf("%d", &array[a][b]);
}//End of for loop
return;
}//End of function
//Display the matrix contents
void printArray(int **array, int r, int c)
{
int a, b;
//Loops till r value
for(a = 0; a < r; a++)
{
//Loops till c value
for(b = 0; b < c; b++)
//Displays data
printf("%4d", array[a][b]);
//New line for each row
printf(" ");
}//End of loop
return;
}//End of function
//Returns 1 if duplicate found otherwise 0
int hasDups(int **array, int r, int c)
{
int a, b, cr = 0, flag = 0, val;
//Loops till r value
for(a = 0; a < r; a++)
{
//Stores the value of row a position and column cr position in variable val
val = array[a][cr++];
//Loops till c value
for(b = 0; b < c; b++)
{
//Checks if the val contest is equal to matrix current position
if(val == array[a][b+1])
{
//Increase the flag by one
flag++;
//Come out of the loop
break;
}//End of if
}//End of inner loop
}//End of outer loop
return flag;
}//End of function
//End of main
int main()
{
//Allocate memory to matrix by calling the function with size specified
int **data = allocateArray(5, 5);
//Accept data
readArray(data, 5, 5);
//Display data
printArray(data, 5, 5);
//Checks duplicate
if(hasDups(data, 5, 5))
printf(" The matrix has duplicate value");
else
printf(" No duplicate found");
return 0;
}//End of main
Sample run1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
The matrix has duplicate value
Sample run2:
1
1
2
3
4
5
6
7
8
9
10
11
2
12
13
14
15
16
17
18
19
20
21
22
23
1 1 2 3 4
5 6 7 8 9
10 11 2 12 13
14 15 16 17 18
19 20 21 22 23
The matrix has duplicate value
//two.c
#include<stdio.h>
#include<stdlib.h>
//Structure to create node
typedef struct node
{
int value;
struct node *next;
}Node;
//To add a node at the beginning
Node * add(Node *head, int data)
{
//Creates a new node
Node *newNode = (Node *)malloc(sizeof(Node));
//Stores data for new node
newNode -> value = data;
//New node next points to head
newNode -> next = head;
//Returns new node
return newNode;
}//End of function
//Calculates and returns how many times the given number is available in the list
int howMany(Node *head, int data)
{
//Creates a temporary node
Node *temp = head;
int a, counter = 0;
//Loops 10 times
for(a = 0; a < 10; a++)
{
//Checks if the current node data is equal to the given data
if(temp -> value == data)
//Increase the counter by one
counter++;
//Move to the next node
temp = temp -> next;
}//End of loop
//Returns counter
return counter;
}//End of function
//Main function
int main()
{
int a, num, count;
//Initializes the head to null
Node *head = NULL;
//Accepts 10 numbers
printf(" Enter 10 values: ");
for(a = 0; a < 10; a++)
{
scanf("%d", &num);
head = add(head, num);
}//End of for loop
//Accepts the number to search in list
printf(" Enter a value: ");
scanf("%d", &num);
count = howMany(head, num);
printf(" Value %d occurs %d times ", num, count);
return 0;
}//End of main
Sample run1:
Enter 10 values: 10 2 23 10 10 5 23 44 10 23
Enter a value: 10
Value 10 occurs 4 times
Sample run2:
Enter 10 values: 10 2 23 10 10 5 23 44 10 23
Enter a value: 23
Value 23 occurs 3 times
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.