Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
here i m giving some code bt during compilation i faced 4 compilation errors ....will u plzz explain me ???
 

#include <stdio.h>
#include <conio.h>
void main()
{
int r,c,i,j;
int x[100][100];
void sparse(int x[][],int r,int c);
printf("Enter the no of rows :");
scanf("%d",&r);
printf("Enter the no of columns :");
scanf("%d",&c);
 
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&x[i][j]);
}
}
sparse(x,r,c);
getch();
}
void sparse(int x[][],int r,int c)
{
int sp[200][200];
int m,n,k=1;
for(m=0;m<r;m++)
{
for(n=0;n<c;n++)
{
if(x[m][n]!=0)
{
sp[k][0]=m+1;
sp[k][1]=n+1;
sp[k][2]=x[m][n];
k++;
}
}
}
sp[0][0]=r;
sp[0][1]=c;
sp[0][2]=k-1;
 
printf("The sparse matrix is :\n");
for(m=0;m<k;m++)
{
for(n=0;n<3;n++)
{
printf("%d\t",sp[m][n]);
}
}
}
Posted 20-Feb-13 10:23am
Edited 20-Feb-13 10:33am
v2
Comments
ThePhantomUpvoter at 20-Feb-13 16:26pm
   
Do you think that maybe it would help us if you told us what the errors are?
wikiabhishek at 20-Feb-13 16:35pm
   
errors are "Size of type 'int[]' is unknown or zero " ....wht does it mean???
ThePhantomUpvoter at 20-Feb-13 16:46pm
   
It means exactly what it says. You are trying to create an int array and have not told the compiler the number of elements that the array will have.
H.Brydon at 23-Feb-13 22:54pm
   
Let me just interject in here briefly. Neither solutions #1, 2, or 3 are implementations of a sparse array. If this is homework or in fact an otherwise mandatory requirement, you will get a "zero" (or equiv) for the solutions. I'd suggest becoming friends with Google and ask it what a sparse array is. Try asking for "sparse array c" or something similar...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Define the arguments as int**x instead of int x[][].
 
[EDIT]
The above is not sufficient as shown in solution #2.
With (multi-dimensional) arrays, you must pass at least all but the first dimensions (you may also pass the first, of course). Or you do the element position calcualtion explicitly.
 
C/C++ require to know all (but the first) dimension to calculate the concrete element. Reason: multi-dimensional arrays are stored in one chunk of memory, where each element is accessed by calculating the respective position within that block of memory: pos = ...((((idx0) * dim1 + idx1) * dim2 + idx2) * ... + ...) ....
 
In your case
- idx0: 0...99
- dim1: 100
- idx1: 0...99
(*no* higher dimension and idices 2,3,4...)
 
The following have identical results, assuming you pass the correct arguments in the function call:

implicitexplicit
void sparse(int x[][100], int rows, int cols)
{
   for(int m = 0; m < rows; ++m)
   {
       for(int n = 0; n < cols; ++n)
       { 
           ...
 
           int v = x[m][n];
           ...
       }
   }
}
void sparse(int **x, int rows, int cols)
{
   for(int m = 0; m < rows; ++m)
   {
       for(int n = 0; n < cols; ++n)
       { 
           ...
           int *first_elem = *x;
           int v = first_elem[m*cols+n];
           ...
       }
   }
}
 
[/EDIT]
 
Cheers
Andi
  Permalink  
v2
Comments
H.Brydon at 22-Feb-13 10:52am
   
You have the right idea but that does not create a sparse array.
Andreas Gieriet at 22-Feb-13 19:00pm
   
The OP did have that in the title, but the concrete question is about a compiler error. Please read the question carefully before voting down... (I assume it was you voting down) ;-)
Cheers
Andi
H.Brydon at 23-Feb-13 18:22pm
   
(I didn't downvote). I considered the title for this page to be part of the problem at hand. Indeed he/she didn't mention sparseness in the text but IMHO it seems to be important.
Andreas Gieriet at 23-Feb-13 19:17pm
   
Yes, the title and the question do not match. So, everyone chooses the one that looks more important...
Cheers
Andi
PS: And my solution is indeed not the complete answer to the question of "compiler error".
PPS: The *title* question on google produces good links: C++ sparse array.
PPPS: Sorry for assuming you downvoting the solution - my apologies.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

The compiler can't generate code for the expresion x[m][n] if it doesn't know what size the columns are.
 
The expression x[m][n] is really just syntactic sugar for:
 
 *( x + m * colsize + n )
 
You need to change your function declaration to:
 
void sparse( int **pArr, int r, int c)
 
and replace all references to x[m][n] with:
 
 *(pArr + m * c + n)
  Permalink  
Comments
H.Brydon at 22-Feb-13 10:53am
   
That does not create a sparse array.
TRK3 at 22-Feb-13 11:37am
   
True, but it solves his problem that the code won't compile. Which is what he's asking about it.
Let him wrestle with the problem of actually making a sparse array and then come back with more questions if he needs more help -- he'll learn a lot more that way.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

I think I can help, but please keep in mind that whenever you right programs you have 2 basic audiences, the computer and people who will try to understand your code and maintain it in the future. In this regard the biggest problem is that you need to indent your code so people can understand what you intended it to mean.
 
From the computer's perspective, the biggest problem is that the computer does not know how big the array 'x' is when you pass it into the routine 'sparse()'.
 
Let's explore how the computer handles memory for an array. In a computer memory is linear, meaning that memory is really just one long list of numbers, with each number having a location in the list called its address. In order to represent a two dimensional array, the compiler puts the first row in the first part of memory, the second row in the next part, etc.. What this means is, for example, an array which is 2 by 3, or to use code
myArray[2][3]
will have two rows of 3 values each. Let's say the array's first row has the values 1, 2, and 3 in it and the second row has the values 4, 5, and 6 in it. In this case, the memory within the computer will look like this: 1, 2, 3, 4, 5, 6.
 
When we write an expression such as
int sample = myArray[1][2];
the compiler must compute the correct address of memory from the coordinates given by using its knowledge of the size of the array. In this case, the compiler knows each row is 3 long, so the memory location being accessed is 1 * 3 + 2, or the 5th memory location within the memory for the array. (Don't forget that arrays are zero-based, meaning that the first row is row number zero.) Thus, in this example, 'sample' will be set to 5.
 
Now we come to the core problem, from the computer's perspective, in your code: because you have not told the compiler in your definition of 'sparse()' how big the array is, the compiler cannot know what math it should do to compute the addresses of each element of the array.
 
One simple fix, which is not recommended, is to tell the compiler how big the array is when passing it into 'sparse()', in both locations. This not recommended because when passing arrays into a function the values are copied, which in this case is a waste of time. There is also a limit in some systems on how much data can be passed into a routine.
 
If this is just a quick test program, and will not need to be maintained in the future, the simplest solution is to make the array 'x' a global by putting it outside the 'main()' line, then not passing it into the 'sparse()' routine at all.
 
There are other issues with your code, for example, it appears that sp[][], which you define with 200 rows of 200 columns, will never have more than the first 3 column used. And from a readability standpoint, my personal rule is to never use single letter variables because they are hard to understand what their meaning is supposed to be.
 
At any rate, I hope my ramblings are instructional, though I have left much up to you to continue debugging. Below is a version of your code, indented for readability, with 'x' made global so 'sparse()' can use it.
#include <stdio.h>
#include <conio.h>

int x[100][100];
 
void main()
{
	int r,c,i,j;
	void sparse(int r,int c);
	printf("Enter the no of rows :");
	scanf("%d",&r);
	printf("Enter the no of columns :");
	scanf("%d",&c);
 
	for(i=0;i<r;i++)>
	{
		for(j=0;j<c;j++)>
		{
			scanf("%d",&x[i][j]);
		}
	}
	sparse(r,c);
	getch();
}
void sparse(int r,int c)
{
	int sp[200][200];
	int m,n,k=1;
	for(m=0;m<r;m++)>
	{
		for(n=0;n<c;n++)>
		{
			if(x[m][n]!=0)
			{
				sp[k][0]=m+1;
				sp[k][1]=n+1;
				sp[k][2]=x[m][n];
				k++;
			}
		}
	}
	sp[0][0]=r;
	sp[0][1]=c;
	sp[0][2]=k-1;
	 
	printf("The sparse matrix is :\n");
	for(m=0;m<k;m++)>
	{
		for(n=0;n<3;n++)
		{
			printf("%d\t",sp[m][n]);
		}
	}
}
  Permalink  
v2
Comments
H.Brydon at 22-Feb-13 10:52am
   
Interesting solution but this is a fully populated array, not a sparse array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 279
1 Nirav Prabtani 253
2 Sergey Alexandrovich Kryukov 209
3 _Amy 165
4 Richard MacCutchan 135
0 OriginalGriff 7,799
1 Sergey Alexandrovich Kryukov 6,966
2 Maciej Los 3,994
3 Peter Leow 3,708
4 CHill60 2,742


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 23 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100