2 Replies - 5283 Views - Last Post: 25 October 2006 - 01:54 AM Rate Topic: -----

#1 Euniceno1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 22-October 06

count letter problem

Post icon  Posted 24 October 2006 - 06:40 PM

The following are the code That I created:
#include <stdio.h>
#include <ctype.h>

int main(void)
{
   int c, i, j, letter[26][26];

   for (i=0; i<26; ++i) /* init array to zero */
	   letter[i] = 0;
   for( j= 0; j<26; ++j) 
		   letter[j] = 0;
   while((c = getchar()) != EOF) /* count the letter */
		if(isupper© && islower©)
		  ++letter[c -> 'A' && c -> 'a'];
   for (i=0; i<26; ++i) 
	 if(i%6==0)
   for ( j= 0; j<26; ++j)
	 if(j%6==0) {
	   printf("\n");
	 printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, letter[i][j]);
   }
   printf("\n\n");
   return 0;
}

and the error I got from the gcc compiler:
letter1.c:In function 'main':
letter1.c:9:error: incompatible types in assignment
letter1.c:11:error: incompatible types in assignment
letter1.c:14:error: parse error before 'A'
letter1.c:14:error: parse error before 'A'
I don't know how to correct it.

edit: added [code] tags ~ jayman9

Is This A Good Question/Topic? 0
  • +

Replies To: count letter problem

#2 dragonlady  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 07-August 05

Re: count letter problem

Posted 24 October 2006 - 09:02 PM

It looks like the problem originates with your declaration of letter[26][26]. So far you have an array of arrays, so the each element is an array of 26 elements. Then you have letter[i] = 0. This would be fine if letter[][] was a one-dimensional array, but here you are accessing an element of a two-dimensional array with one parameter. Think of it this way: if I had a two-d graph and I was trying to get a point on that graph, I'd have to supply an x and a y coordinate; anything else would be an error.

I'd use a nested loop on this one. For example, if I had a 2-d array of numbers, and I wanted to have the all first row elements store 1, the second row 2, etc...this is the kind of loop I'd use:
int numbers[3][3];

int num = 0;
for(i = 0; i < 3; i++)//i is a row counter
{
	  num++;
	  for(j = 0; j<3; j++){
		  numbers[i][j] = num;  
		  
	  }
}

for(i = 0; i < 3; i++){
	  for(j = 0; j<3; j++){
		 cout<<numbers[i][j]; 
	  }
	  cout<<" ";
}


Was This Post Helpful? 0
  • +
  • -

#3 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: count letter problem

Posted 25 October 2006 - 01:54 AM

[quote name='Euniceno1' date='24 Oct, 2006 - 06:40 PM' post='178938']
The following are the code That I created:
#include <stdio.h>
#include <ctype.h>

int main(void)
{
   int c, i, j, letter[26][26];

   for (i=0; i<26; ++i) /* init array to zero */

   for( j= 0; j<26; ++j)
 	
   while((c = getchar()) != EOF) /* count the letter */
		if(isupper© && islower©) 

   for (i=0; i<26; ++i) 
	 if(i%6==0)
   for ( j= 0; j<26; ++j)
 if(j%6==0) {
	   printf("\n");
	 printf("%4c%4c:%3d%3d", 'A' || 'a' +i +j, letter[i][j]);
   }
   printf("\n\n");
   return 0;
}

and the error I got from the gcc compiler:
letter1.c:In function 'main':
letter1.c:9:error: incompatible types in assignment
letter1.c:11:error: incompatible types in assignment
letter1.c:14:error: parse error before 'A'
letter1.c:14:error: parse error before 'A'
I don't know how to correct it.

edit: added
 tags ~ jayman9
[/quote]
Euniceno

The compiler gave you line numbers....
[code]
File		  which function
letter1.c : In function 'main':
						  the problem description.....
letter1.c:9:error: incompatible types in assignment
			 ^ line number...


Lets get those lines down here...
#  Line
9  letter[i] = 0;
11 letter[j] = 0;
14 ++letter[c -> 'A' && c -> 'a'];


Now we can see a trend in the code.. Dragonlady has hit the nail on the head.

Letter[i] simply doesn't match the definition letter[][].
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1