-
quick question
Hey everyone could someone tell me why the first code is working(without functions) and the second one is not?
Code:
#include<stdio.h>
#include <stdlib.h>
int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int **A;
int i = 0, j = 0;
char a,c;
do
{
A = malloc(2 * sizeof (int *));
printf("\ncmd> ");
c = getchar();
if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
if (A[i][j] == 99) // 'x' is character variable i declared
break;
}
}
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
}
while(flag == 1);
return 0;
}
same code using functions
Code:
#include<stdio.h>
#include <stdlib.h>
int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int **A=NULL;
int i = 0, j = 0;
char a,c;
do
{
A = malloc(2 * sizeof (int *));
printf("\ncmd> ");
c = getchar();
if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
}
}
matrix_display(A[0], row_size1, col_size1);
}
if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[1], row_size1, col_size1);
}
}
while(flag == 1);
//return 0;
}
void matrix_display(int *A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
-
The first one might 'work', but it's still broken none the less.
Code:
$ valgrind ./a.out
==4438== Memcheck, a memory error detector
==4438== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==4438== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==4438== Command: ./a.out
==4438==
cmd> A 3 2
==4438== Invalid write of size 8
==4438== at 0x400786: main (bar.c:26)
==4438== Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
==4438== at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4438== by 0x4006FB: main (bar.c:14)
==4438==
1 2 3
4 5 6
==4438== Invalid read of size 8
==4438== at 0x4007B4: main (bar.c:31)
==4438== Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
==4438== at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4438== by 0x4006FB: main (bar.c:14)
==4438==
The required matrix is
1 2
3 4
==4438== Invalid read of size 8
==4438== at 0x40081A: main (bar.c:40)
==4438== Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
==4438== at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4438== by 0x4006FB: main (bar.c:14)
==4438==
5 6
==4438== Invalid read of size 8
==4438== at 0x400878: main (bar.c:45)
==4438== Address 0x51d2050 is 0 bytes after a block of size 16 alloc'd
==4438== at 0x4C28F9F: malloc (vg_replace_malloc.c:236)
==4438== by 0x4006FB: main (bar.c:14)
==4438==
cmd>
Look at this code.
Code:
#include<stdio.h>
#include <stdlib.h>
int main(void)
{
int row_size1, col_size1, row_size2, col_size2, flag = 1;
int **A[2];
int i = 0, j = 0;
char a, c;
do {
// A = malloc(2 * sizeof(int *));
printf("\ncmd> ");
c = getchar();
if (c == 'A') {
scanf("%d %d", &row_size1, &col_size1);
A[0] = malloc(row_size1 * sizeof(int *));
for (i = 0; i < row_size1; i++) {
A[0][i] = malloc(col_size1 * sizeof(int));
}
for (i = 0; i < row_size1; i++) {
for (j = 0; j < col_size1; j++) {
scanf("%d", &A[0][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
printf("The required matrix is\n");
for (i = 0; i < row_size1; i++) {
for (j = 0; j < col_size1; j++)
printf("%d ", A[0][i][j]);
printf("\n");
}
for (i = 0; i < row_size1; i++) {
free(A[0][i]);
}
free(A[0]);
}
}
while (flag == 1);
return 0;
}
If you want, you can start with
int ***A;
and
A = malloc( 2 * sizeof(int**) );
to replace the array of 2 elements.
-
Oh I thought the first one was perfect but thanks
but why isn't the second one even compiling? I tried the int ***A
but i honestly thought that this line
Code:
A = malloc( 2 * sizeof(int**) );
just tells the program I am allocating space for 2 matrices A and B
-
Well if you turned everything into pointers, then you need
void matrix_display(int **A, int row_size1, int col_size1)
-
is this what you mean?
Code:
#include<stdio.h>
#include <stdlib.h>
int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int **A=NULL;;
int i = 0, j = 0;
char a,c;
do
{
A = malloc(2 * sizeof (int **));
printf("\ncmd> ");
c = getchar();
if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[0][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[0][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[0][i][j], row_size1, col_size1);
}
if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[1][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[1][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[1][i][j], row_size1, col_size1);
}
}
while(flag == 1);
}
void matrix_display(int **A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
-
No, nothing like what I meant.
int ***A;
A is a pointer to a number of matrices
A[0] is a pointer to the first matrix
A[0][0] is a pointer to the first row of the first matrix.
To display one of the matrices, you would do
matrix_display(A[1], row_size1, col_size1);
I suggest you prototype the function before calling it, then you would get some useful diagnostics.
Though TBH, I can't see how this compiles at all without a great deal of complaining from the compiler.
FWIW, matrix_display seems good now, if you can figure out how to call it properly.
Perhaps you should start with
int **A[2];
and get that working properly, before trying to allocate something for int ***A;
-
please tell me this is what you mean
way too many warnings! :(
Code:
#include<stdio.h>
#include <stdlib.h>
void matrix_display(int *matrix, int row_count, int column_count);
int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int **A[2];
int i = 0, j = 0;
char a,c;
do
{
A = malloc(2 * sizeof (int **));
printf("\ncmd> ");
c = getchar();
if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[0][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[0][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[0], row_size1, col_size1);
}
if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[1][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[1][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[1], row_size1, col_size1);
}
}
while(flag == 1);
//return 0;
}
void matrix_display(int *A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
-
> void matrix_display(int *matrix, int row_count, int column_count);
Are you paying even the slightest bit of attention?
In my last post, I said matrix_display was OK, and you go and muck it up again!
Code:
$ gcc -Wall bar.c
bar.c: In function ‘main’:
bar.c:15:8: warning: unused variable ‘a’ [-Wunused-variable]
bar.c:12:37: warning: unused variable ‘col_size2’ [-Wunused-variable]
bar.c:12:27: warning: unused variable ‘row_size2’ [-Wunused-variable]
bar.c:92:1: warning: control reaches end of non-void function [-Wreturn-type]
$
$ valgrind ./a.out
==6204== Memcheck, a memory error detector
==6204== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==6204== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==6204== Command: ./a.out
==6204==
cmd> A
3 2
1 2 3
4 5 6
The required matrix is
1 2
3 4
5 6
cmd>
cmd> ^C==6204==
==6204== HEAP SUMMARY:
==6204== in use at exit: 0 bytes in 0 blocks
==6204== total heap usage: 4 allocs, 4 frees, 48 bytes allocated
==6204==
==6204== All heap blocks were freed -- no leaks are possible
==6204==
==6204== For counts of detected and suppressed errors, rerun with: -v
==6204== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
TWO changes.
1. fix the prototype of matrix_display
2. remove line A = malloc(2 * sizeof (int **));
-
So I did what you said I got warnings but... THE program works!
in total I need 4 variables A B C and D so I added for C and D and once again we're back to square 1
Code:
#include<stdio.h>
#include <stdlib.h>
void matrix_display(int *matrix, int row_count, int column_count);
int main (void)
{
int row_size1,col_size1,row_size2,col_size2,flag =1;
int ****A[4];
int i = 0, j = 0;
char a,c;
do
{
// A = malloc(2 * sizeof (int **));
printf("\ncmd> ");
c = getchar();
if(c == 'A')
{
scanf("%d %d",&row_size1,&col_size1);
A[0]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[0][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[0][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[0], row_size1, col_size1);
}
if(c == 'B')
{
scanf("%d %d",&row_size1,&col_size1);
A[1]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[1][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[1][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[1], row_size1, col_size1);
}
if(c == 'C')
{
scanf("%d %d",&row_size1,&col_size1);
A[2]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[2][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[2][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[2], row_size1, col_size1);
}
if(c == 'D')
{
scanf("%d %d",&row_size1,&col_size1);
A[3]= malloc(row_size1*sizeof(int*));
for(i=0;i<row_size1;i++)
{
A[3][i]=malloc(col_size1*sizeof(int));
}
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
{
scanf("%d",&A[3][i][j]);
// if (A[i][j] == 99) // 'x' is character variable i declared
// break;
}
}
matrix_display(A[3], row_size1, col_size1);
}
}
while(flag == 1);
//return 0;
}
void matrix_display(int ****A, int row_size1, int col_size1)
{
int i,j;
printf("The required matrix is\n");
for(i=0;i<row_size1;i++)
{
for(j=0;j<col_size1;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<row_size1;i++)
{
free(A[i]);
}
free(A);
}
-
I made the same changes right?
-
what thing did i miss again?
-
Quote:
Originally Posted by
zafy
Code:
void matrix_display(int *matrix, int row_count, int column_count);
void matrix_display(int ****A, int row_size1, int col_size1)
The prototype doesn't match the definition, and now you have... how many stars in there?
-
Yes! I got it
honestly guys thank you so much I appreciate it so much :)
-
I asked you to do it on the last topic you posted - Can you PLEASE fix your indentation
This is my preferred style - Indent style - Wikipedia, the free encyclopedia