This is the initialising part of a program im writing. I am new ish to c and am quite confused with one of the outputs I am getting. Any advice would be greatly appreciated
for input 9 11 I get
1 2 3 4 5 6 7 8 9
9 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
when I expect to get
1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
void *safeMalloc(int n) {
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
int **makeIntArray2D(int width, int height) {
int **arr = safeMalloc(height*sizeof(int *));
for (int row=0; row < height; row++) {
arr[row] = safeMalloc(width*sizeof(int));
}
return arr;
}
int main(int argc, char *argv[]) {
int numDisks;
int numMoves;
scanf("%d %d", &numDisks, &numMoves);
int **tohan = makeIntArray2D(3, numDisks);
for(int i = 0; i < 3; i++) {
for(int j =0; j < numDisks; j++) {
tohan[i][j] = 0;
}
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < numDisks; j++) {
printf("%d ", tohan[i][j]);
}
printf("\n");
}
printf("\n");
for(int k = 0; k < numDisks; k++) {
tohan[0][k] = k+1;
}
for(int i = 0; i < 3; i++) {
for(int j = 0; j < numDisks; j++) {
printf("%d ", tohan[i][j]);
}
printf("\n");
}
printf("\n");
}
You have:
int **makeIntArray2D(int width, int height) { … }
You call:
int **tohan = makeIntArray2D(3, numDisks);
You expect the height of the array to be 3 and the width 9, but you are passing the parameters in the wrong sequence for the function.
Note: this diagnosis could not be made without seeing the code for makeIntArray2D().
Related
I'm trying to write an Count sort Program. My Problem is that it seems to sort the first 4 Numbers and after that he only prints 0. My input comes from an external file.
This is my sorted output:
0 1 5 8 0 0 0 0 0 0 0 0 0 0 0 0 0
The following is my code so far:
int MAX_LAENGE = 1000;
int MAX_VALUE = 100;
int i, k, j;
void count_sort_calculate_counts(int input_array[], int len, int count_array[]) {
for ( i = 0; i < len; i++)
{
count_array[i] = 0;
}
for (j = 0; j < len; j++)
{
count_array[input_array[j]] = count_array[input_array[j]] + 1;
}
}
void count_sort_write_output_array(int output_array[], int len, int count_array[]) {
k = 0;
for (j = 0; j < len; j++)
{
for (i = 0; i < count_array[j]; i++)
{
output_array[k] = j;
k = k + 1;
}
}
Two of the cycles - the one setting the counts to 0 and the one printing the numbers should not be until len, instead they should be until MAX_VALUE.
for ( i = 0; i < MAX_VALUE; i++)
{
count_array[i] = 0;
}
Similarly in the function that prints the numbers:
for (j = 0; j < MAX_VALUE; j++)
Without having more context around how you use this code, this is the only issue I spot.
This below is my ds hw code.
#include <stdio.h>
typedef struct
{
int row;
int col;
int val;
}term;
void CreateTriplet(term t[],int a[][10],int m, int n)
{
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j ++)
{
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k)
{
int i;
for(i = 0 ; i < k ; i++)
{
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[])
{
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++)
{
for (j = 1; j <=t1[0].val; j++)
{
if (t1[j].col == i)
{
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main()
{
int a[10][10], i, j, m, n, zero=0, nonzero;
term t1[101], t2[102];
scanf("%d %d", &m, &n);
for(i = 0; i < m ;i++)
{
for(j= 0; j < n ;j++)
{
scanf("%d]\n", &a[i][j]);
if(a[i][j]==0)
zero++;
}
}
if (i*j != i*i && j*j) /* I have no idea how to modify this section for the output*/
{
printf("Input matrix has wrong size. Please input again. \n");
}
else
{
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero+1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero+1);
}
return 0;
}
The main problem is in the section (if statement.)
When I was writing the if conditional, the input size was 6 * 6. Also, the elements in the matrix are not 6 * 6. My idea is to let the condition determine row * col != input elements. I don't know how to modify the if statement. Can someone help me?
**Input**
55
15 0 0 22 0
0 0 3 0 0
0 0 0 -6 0
0 0 0 0 7
91 0 0 0 0
**Output**
Sparse matrix by triplet form:
5 5 6
0 0 15
0 3 22
1 2 3
2 3 -6
3 4 7
4 0 91
Transpose of the sparse matrix:
5 5 6
0 0 15
0 4 91
2 1 3
3 0 22
3 2 -6
4 3 7
**Input**
66
15 0 0 22 0 -15 100
0 11 3 0 0 0
0 0 0 -6 0 0
0 0 0 0 0 0
91 0 0 0 0 0
0 0 28 0 0 0
1 0 0 0 0 0
**Output**
Input matrix has wrong size. Please input again.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int row;
int col;
int val;
} term;
void CreateTriplet (term t[],int a[][10],int m, int n) {
int i, j, k = 0;
t[0].row = m;
t[0].col = n;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j ++) {
if(a[i][j] == 0)
continue;
k++;
t[k].row = i;
t[k].col = j;
t[k].val = a[i][j];
}
}
t[0].val = k;
}
void printTriplet(term t[], int k) {
int i;
for(i = 0 ; i < k ; i++) {
printf("%d\t", t[i].row);
printf("%d\t", t[i].col);
printf("%d\n", t[i].val);
}
}
void findTranspose(term t1[], term t2[]) {
int i, j, k;
t2[0].row = t1[0].col;
t2[0].col = t1[0].row;
t2[0].val = t1[0].val;
k = 1;
for (i = 0; i < t1[0].col; i++) {
for (j = 1; j <=t1[0].val; j++) {
if (t1[j].col == i) {
t2[k].row = t1[j].col;
t2[k].col = t1[j].row;
t2[k].val = t1[j].val;
k++;
}
}
}
}
int main (int argc, char * *argv, char * *envp) {
int a[10][10], i, j, m, n, zero = 0, nonzero;
term t1[101], t2[102];
int ok_or_not = 1;
while (ok_or_not == 1) {
scanf ("%d %d", &m, &n);
if (m * n != m * m) {
printf ("Input matrix has wrong size. Please input again. \n");
}
else {
ok_or_not = 0;
}
}
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d]\n", &a[i][j]);
if (a[i][j] == 0)
zero++;
}
}
if (i * j != i * i && j * j) { /* I have no idea how to modify this section for the output*/
} else {
nonzero = m * n - zero;
printf("Sparse matrix by triplet form:\n");
CreateTriplet(t1, a, m, n);
printTriplet(t1, nonzero + 1);
findTranspose(t1, t2);
printf("Transpose of the sparse matrix:\n");
printTriplet(t2, nonzero + 1);
}
return (EXIT_SUCCESS);
}
Below is the code for resource request. My safety algorithm is running fine but when I ask for additional resources it is giving error situation(request>need). But when I practically do it than I am unable to find any error.
here is my code
#include<stdio.h>
#include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
show();
cal();
request();
getch();
return 0;
}
void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Max\t Available\t");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}
void cal() {
int finish[100], temp, need[100][100], flag = 1, k, c1 = 0;
int safe[100];
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\n The system is in safe state");
} else {
printf("\n Process are in dead lock");
printf("\n System is in unsafe state");
}
}
void request() {
int c, pid, request[100][100], B[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n ******Error encountered******\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
getch();
} else {
exit(0);
}
}
OUTPUT for the above code:
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
Process Allocation Max Available
P1 0 1 0 7 5 3 3 3 2
P2 2 0 0 3 2 2
P3 3 0 2 9 0 2
P4 2 1 1 2 2 2
P5 0 0 2 4 3 3
P1->P3->P4->P2->P0->
The system is in safe state
Do you want make an additional request for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
******Error encountered******
OUTPUT that is needed:
*********DEADLOCK AVOIDANCE USINGBANKER'S ALGORITHM***********
Enter total number of processes : 5
Enter total number of resources : 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0
2
2 1 1
0 0 2
Available resources :
Resource 1 : 3
Resource 2 : 3
Resource 3 : 2
********Maximum Requirement Matrix*********
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
********Allocation Matrix**********
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A safety sequence has been detected.
P1 P3 P4 P0 P2
Do you want make an additionalrequest for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
A safety sequence has been detected.
P1 P3 P4 P0 P2
Where can I make changes to get the above output.
Having done some cleanup on your code it appears that your problem is that in if (request[0][i] > need[pid][i]) {, you are looking at the values,
request[0][0] = 1
request[0][1] = 2
request[0][2] = 1
However when you compare this against need you are comparing against need[2][i], the values of i at this time are,
need[2][0] = 6
need[2][1] = 0
need[2][2] = 0
As such when i=1 and when i=2 the values 1 and 2 are more than 0 and 0, as such the error occurs.
I've modified the output to show you the values in need,
********** Banker's Algo ************
Enter the no of Processes 5
Enter the no of resources instances 3
Enter the Max Matrix
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the Allocation Matrix
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
Enter the available Resources
3 3 2
P1->P3->P4->P2->P0->
The system is in safe state
Process Allocation Need Max Available
P1 0 1 0 7 5 3 7 4 3 8 8 7
P2 2 0 0 3 2 2 1 2 2
P3 3 0 2 9 0 2 6 0 0
P4 2 1 1 2 2 2 0 1 1
P5 0 0 2 4 3 3 4 3 1
Do you want make an additional request for any of the process ? (1=Yes|0=No)1
Enter process number : 2
Enter additional request :
Request for resource 1 : 1
Request for resource 2 : 2
Request for resource 3 : 1
******Error encountered******
Code,
#include<stdio.h>
#include<stdlib.h>
//include<conio.h>
int max[100][100];
int alloc[100][100];
int need[100][100];
int avail[100];
int n, r;
void input();
void show();
void cal();
void request();
int main() {
int i, j;
printf("********** Banker's Algo ************\n");
input();
cal();
show();
request();
//getch();
return 0;
}
void input() {
int i, j;
printf("Enter the no of Processes\t");
scanf("%d", &n);
printf("Enter the no of resources instances\t");
scanf("%d", &r);
printf("Enter the Max Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &max[i][j]);
}
}
printf("Enter the Allocation Matrix\n");
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
scanf("%d", &alloc[i][j]);
}
}
printf("Enter the available Resources\n");
for (j = 0; j < r; j++) {
scanf("%d", &avail[j]);
}
}
void show() {
int i, j;
printf("Process\t Allocation\t Need\t Max\t Available");
for (i = 0; i < n; i++) {
printf("\nP%d\t ", i + 1);
for (j = 0; j < r; j++) {
printf("%d ", alloc[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", max[i][j]);
}
printf("\t");
for (j = 0; j < r; j++) {
printf("%d ", need[i][j]);
}
printf("\t");
if (i == 0) {
for (j = 0; j < r; j++)
printf("%d ", avail[j]);
}
}
}
void cal() {
int finish[100], flag = 1, k, c1 = 0;
int i, j;
for (i = 0; i < n; i++) {
finish[i] = 0;
}
//find need matrix
for (i = 0; i < n; i++) {
for (j = 0; j < r; j++) {
need[i][j] = max[i][j] - alloc[i][j];
}
}
printf("\n");
while (flag) {
flag = 0;
for (i = 0; i < n; i++) {
int c = 0;
for (j = 0; j < r; j++) {
if ((finish[i] == 0) && (need[i][j] <= avail[j])) {
c++;
if (c == r) {
for (k = 0; k < r; k++) {
avail[k] += alloc[i][j];
finish[i] = 1;
flag = 1;
}
printf("P%d->", i);
if (finish[i] == 1) {
i = n;
}
}
}
}
}
}
for (i = 0; i < n; i++) {
if (finish[i] == 1) {
c1++;
} else {
printf("P%d->", i);
}
}
if (c1 == n) {
printf("\n The system is in safe state\n");
} else {
printf("\n Process are in dead lock\n");
printf("\n System is in unsafe state\n");
}
}
void request() {
int c, pid, request[100][100], i;
printf("\n Do you want make an additional request for any of the process ? (1=Yes|0=No)");
scanf("%d", &c);
if (c == 1) {
printf("\n Enter process number : ");
scanf("%d", &pid);
printf("\n Enter additional request : \n");
for (i = 0; i < r; i++) {
printf(" Request for resource %d : ", i + 1);
scanf("%d", &request[0][i]);
}
for (i = 0; i < r; i++) {
if (request[0][i] > need[pid][i]) {
printf("\n ******Error encountered******\n");
exit(0);
}
}
for (i = 0; i < r; i++) {
avail[i] -= request[0][i];
alloc[pid][i] += request[0][i];
need[pid][i] -= request[0][i];
}
cal();
//getch();
} else {
exit(0);
}
}
In this code I took two 1D arrays and kept multiplicand and multipliers in those.
Then I kept the multiplication results in a 2d array to add them.
{
int n, m, i, j;
printf("Enter multiplicand(n) size: ");
scanf("%d", &n);
printf("Enter multiplier(m) size: ");
scanf("%d", &m);
int a[n], b[m];
printf("Enter multiplicands: ");
for(i = 0; i<n; i++)
{
scanf("%d", &a[i]);
}
printf("Enter multipliers: ");
for(i = 0; i<m; i++)
{
scanf("%d", &b[i]);
}
int c[m][n+m] , k = 0 , l = m+n-1 , p = 2;
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
}
}
for(i = m-1; i>=0; i--)
{
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
l--;
}
l = m+n-p;
p++;
k++;
}
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
I was able to get -1's on the top left but I want 0's in the right bottom of the numbers
So, if I input a[5] = {1 , 1 , 1 , 1 , 1} , b[5] = {1 , 1 , 1 , 1 , 1}
I should get:
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 1 1 1 1 1 0
-1 -1 -1 1 1 1 1 1 0 0
-1 -1 1 1 1 1 1 0 0 0
-1 1 1 1 1 1 0 0 0 0
Here is some cleaner code with print outs. I really don't understand what you are trying to do. The whole -1 loop is confusing and the output example is not clear.
I think you are trying to just add the multiplication result to a new array? But what does the -1 mean?
#include <stdio.h>
int main()
{
int n, m, i,l, j,k,p;
n =5;
m = 5;
int a[5] = {1}, b[5] = {1};
int c[m][n+m];
k = 0;
l = m+n-1;
p = 2;
for(i = 0; i<m; i++)
{
printf("\n");
for(j = 0; j<m+n; j++)
{
c[i][j] = -1;
printf("[%i][%i]:%i ",i,j,c[i][j]);
}
}
printf("\n");
for(i = m-1; i>=0; i--)
{
printf("\n");
for(j = n-1; j>=0; j--)
{
c[k][l] = a[j]*b[i];
printf("[%i][%i]: %i x %i = %i ",k,l,a[j],b[i],c[k][l]);
l--;
}
l = m+n-1;
k++;
}
printf("\n\n");
for(i = 0; i<m; i++)
{
for(j = 0; j<m+n; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
If you explain more about what you are trying to do then I can edit this to help.
For example, if your 'a' array is {2,4} and your 'b' array is {3,6}. What is the expected output of the 'c' array.
c = { ? }
The size of the square matrix is entered as standard input (the range of input values is 2 to 10). Create a program that prints the unit matrix of the input size.
The unit matrix is a matrix whose diagonal from the top left to the bottom right (main diagonal) is 1 and all others are zero.
ex) input: 5
result :
1 0 0 0 0\n
0 1 0 0 0\n
0 0 1 0 0\n
0 0 0 1 0\n
0 0 0 0 1\n
my code:
int main(){
int n;
scanf("%d", &n);
int **matrix = malloc(sizeof(int *) * n);
for (int i = 0; i < n; i++)
{
matrix[i] = malloc(sizeof(int) * n);
memset(matrix[i], 0, sizeof(int) * n);
}
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1;
printf("%d ", matrix[i][i]);
}
printf("\n");
for (int i = 0; i < n; i++)
{
free(matrix[i]);
}
free(matrix);
return 0;
}
.. what 's the error?
You need a double loop to print the output:
for (int i = 0; i < n; i++)
{
matrix[i][i] = 1; // set identity
}
for (int j=0; j<n; j++)
{
for (int i = 0; i < n; i++)
{
printf("%d ", matrix[j][i]);
}
printf("\n");
}
One error is to think yo need a data strucure to print the output requested.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int n;
if (scanf("%d", &n) != 1 || n < 2 || 10 < n) {
perror("Input error!\n\n");
return EXIT_FAILURE;
}
for (int i = 0; i < n; ++i) {
for (int k = 0; k < n; ++k)
printf(i == k ? "1 " : "0 ");
putchar('\n');
}
}