1

I have been struggling for a while with my program. I am trying to find horizontal pairs in an array that is setup in function1. My goal is to change the original array in another function. Then process that array to find a horizontal pair.

One problem that has occurred is when I run the program, the result is zero. Another problem is the gcc warning in function 1, warning: comparison between pointer and integer. The other problem is another gcc warning (marked by the **) warning: passing argument 1 of 'function1' from incompatible pointer type.

I appreciate any help, as a beginner, I have spent several hours on this problem and have tried to find solutions, but trying to use pointers and using struct and typedef have not worked. :(

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void function1 (int letter1[][16]);
void function2 (int letter2[][16]);

int  main ( void )
{  

  int letter_array [13][16];

  printf("\n \t\t Hello!");
  printf("\n This program will create a random selection of 180 upper-case"
    " characters. \n");

  **function1(&letter_array);**

  function2(letter_array);

return ( 0 ) ;
}    


void function1 (int letter1 [][16])
{
  int i, z; 
  srandom((unsigned)time(NULL));

  for(i=0; i<=11; i++)
    {
       for(z=0; z<=14; z++)
       {
          letter1 [i][z] = random( )%26+65;
          printf("%c ", letter1 [i][z]);
       }
       printf("\n");
    }

  return ;
  }

void function2 (int letter2 [][16])
  {
  int a,b; 
  int m=0;
    for( a = 0; a <= 11; a++)
    {
       for( b = 0 ; b <= 14; b++)
       {
            if (letter2 == (letter2 + 1))
            m++;
       }
    }
  printf("\nThe number of horizontal pairs of characters"
    " are: %d", m);

  return ;

2 Answers 2

2

Just remove the ampersand & from the argument.

Change

function1(&letter_array);

to

function1(letter_array);

EDIT: Also change

if (letter2 == (letter2 + 1))

to

if (letter2[a][b] == (letter2[a][b+1]))
Sign up to request clarification or add additional context in comments.

6 Comments

Probably also better to use letter1[i][z] = random() % 26 + 'A'; instead of the magic number 65.
what difference would it make?
Readability to indicate what the code is trying to do. The magic number doesn't make it immediately clear why he's adding the numbers and what they represent. I'd even go so far as to write it as (char)(random() % 26 + 'A') with a comment that says "Generate a random letter from A-Z"
@Tawnos I thought about doing that, but I was not sure of the shell that would be used by others to compile the program. Harry's book has said some shells do look order from A-Z, others will order from Aa-xZ. The shell I use, the random letter A-Z would have been fine.
@ skrtbhtngr You're right. I don't know why I couldn't put it together, but I had the sneaking suspicion that my array wasn't being changed in function1. So, my efforts were mostly in "fixing" the relation between function1 and main().
|
1

The warning is occurring because you are passing a pointer to an array[][16] into function1 instead of the array itself. This can be resolved by removing the &:

function1(letter_array);

The program is returning 0 because of the return statement at the end of your main function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.