1

I know it looks like a lot but it really isn't
It just checks if a char from an array is a vowel and if it's on a main/supporting diagonal it makes the counter go +1.

The problem is the output is Main diagonal = 31, Supporting diagonal = 4. I'm not sure where the problem is and i've been looking at this for an hour now.

#include <stdio.h>
#include <math.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

void Dijagonala(char znakovi[5][5],int *glav,int *spor)
{
    int i,j;
for(i=0;i<5;i++){ 
for(j=0;j<5;j++)
{
    if (((znakovi[i][j] == 'a') || (znakovi[i][j] == 'e') || (znakovi[i][j] == 'i') || (znakovi[i][j] == 'o') || (znakovi[i][j] == 'u') ||
    (znakovi[i][j] == 'A') || (znakovi[i][j] == 'E') || (znakovi[i][j] == 'I') || (znakovi[i][j] == 'O') || (znakovi[i][j] == 'U')) 
    && (i==j))
    *glav+=*glav+1;
else if (((znakovi[i][j] == 'a') || (znakovi[i][j] == 'e') || (znakovi[i][j] == 'i') || (znakovi[i][j] == 'o') || (znakovi[i][j] == 'u') ||
    (znakovi[i][j] == 'A') || (znakovi[i][j] == 'E') || (znakovi[i][j] == 'I') || (znakovi[i][j] == 'O') || (znakovi[i][j] == 'U')) 
    && (j+i == 4))
    *spor=*spor+1;

}
}
}
int main()
{
char znakovi[5][5];
int gsam=0,ssam=0,i,j,test=5;
for(i=0;i<5;i++){ 
for(j=0;j<5;j++)
{scanf("%c",&znakovi[i][j]);
fflush(stdin);
}}
Dijagonala(znakovi,&gsam,&ssam);
printf("glavna %d \n Sporedna %d",gsam,ssam);
}
Morty C-137
  • 111
  • 10
  • Please explain `*glav+=*glav+1;`. It looks like it wants to be `(*glav)+=1;`. – Yunnosch Apr 25 '17 at 18:30
  • `fflush(stdin);` - [don't do that](https://stackoverflow.com/questions/2979209/using-fflushstdin). – WhozCraig Apr 25 '17 at 18:31
  • @Yunnosch That fixed it! Thank you.I really overlooked that one. – Morty C-137 Apr 25 '17 at 18:34
  • A suggestion: `int is_vowel = strchr( "aeiouAEIOU", znakovi[i][j]) != 0;` – Michael Burr Apr 25 '17 at 18:42
  • @MichaelBurr Thanks for the tip! Will do in the future. – Morty C-137 Apr 25 '17 at 18:47
  • @WhozCraig I've read that before but in this instance what are my alternatives? The task says to make a char 2d array and if i just do a for loop with scanf it sees the \n that i press as an input. – Morty C-137 Apr 25 '17 at 18:50
  • @MrPoopyButthole Consume `stdin` via `fgetc` through either the next `'\n'` or EOF, whichever comes first. Alternatively, you could use `fgets` and trim the retained newline from the received data. The latter isn't so attractive if you're intentionally discarding data between what you're reading and end-of-line, however. – WhozCraig Apr 25 '17 at 18:53

1 Answers1

1
*glav+=*glav+1;

looks like it wants to be

(*glav)+=1;  

Similar for

*spor=*spor+1;

I.e. do not increase a variable by its value and 1.
Instead only increase by 1.
And (my personal taste) be more generous with ().

Yunnosch
  • 21,438
  • 7
  • 35
  • 44