-2

So as you guys see in the question, I have this issue. I mean, I really can't see whats the problem, maybe it is something stupid, but I can't figure it out. So here is the code :

#include <stdio.h>
int main() {
    int i,n;
    int niz[100];
    int brojac[100]={0};
    int maxi,mini;
    printf("Sada unesi clanove niza: \n");
    for(i=0;i<100;i++) {
        scanf("%d",&niz[i]);
        if(niz[i]==-1) {
            printf("Unijeli ste -1, a to znaci kraj unosenja clanova niza.\n");
            break;
        }
        if(niz[i]<0 || niz[i]>100){
            printf("Pogresan unos. On se nece pikati.\n");
            i--;
        }
    }
    n=i;

    //POVECAVAMO VRIJEDNOST NEKIH CLANOVA HISTOGRAMA(SVI CLANOVI VEC POSTOJE, I INICIJALIZOVANI SU NULOM)
    for(i=0;i<n;i++) {
        brojac[niz[i]]++;
    }
    maxi=0;
    mini=-1;

    //Nadji INDEX NAJVECEG ELEMENTA HISTOGRAMA
    for(i=0;i<=100;i++) {
        if(brojac[i]>brojac[maxi]) {
            maxi=i;
        }
        if(brojac[i]>0 && (mini==-1 || brojac[i]<brojac[mini])) { 
            mini=i;
        }
    }

    printf("Element niza koji se ponavlja najveci broj puta(a koji je i najamnji ako takvih brojeva ima vise je: %d\n",maxi);
    printf("Element niza koji se ponavlja najmanji broj puta(a koji je i najamnji ako takvih brojeva ima vise je: %d",mini);
    return 0;
}
Nick S
  • 1,299
  • 1
  • 11
  • 23
Cluv
  • 27
  • 5
  • so what is your question? How do I debug this code? Answer - use your debugger – pm100 Oct 19 '18 at 17:24
  • if you do not know how to use your debugger please say what compiler and tools you are using - we can point you at the instructions for using it – pm100 Oct 19 '18 at 17:26
  • Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Oct 19 '18 at 17:26
  • Codeblocks. I never understood how debugger works ;( – Cluv Oct 19 '18 at 17:28
  • `for(i=0;i<=100;i++)` -> `for(i=0;i<100;i++)`. You are accessing the array out of bounds. – Osiris Oct 19 '18 at 17:28
  • My bad. This array brojac should have 101 elements, not 100. and then the for loop wil work properly – Cluv Oct 19 '18 at 17:43
  • 1
    @farC - first thing. Learn to use the debugger, stop writing code right now. Use the debugger on this program. https://www.youtube.com/watch?v=h_r5ZfETRZQ - not sure if this is any good but google shows many tutorials – pm100 Oct 19 '18 at 17:47
  • and here https://stackoverflow.com/questions/23441964/how-to-debug-in-code-blocks – pm100 Oct 19 '18 at 17:50

2 Answers2

0

So, the program is trying to determine the most reoccurring number entered between 1 and 100 inclusive. You method is to store the 100 numbers in an array niz[], then traverse that array and store the number of times a digit occurred in the equivalent index in another array brojac[]. So if nix[1]= 8 then brojac[8]++;

Another solution is to read the digit into a temp value and if it's within range, then increase the value stored at the index of the number in the array. Be sure to initialize all values on nix[] to zero first.

Example: 3 is entered. 3 is stored in temp. 3 is within range so niz[temp]= niz[temp]++;

But anyway, the problem is

maxi=0;
mini=-1;

//Nadji INDEX NAJVECEG ELEMENTA HISTOGRAMA
for(i=0;i<=100;i++) {// here
    if(brojac[i]>brojac[maxi]) {
        maxi=i;
    }
    if(brojac[i]>0 && (mini==-1 || brojac[i]<brojac[mini])) { // and  here
        mini=i;
    }
}

that line 'if(brojac[i]>0 && (mini==-1 || brojac[i]

for the first case where i=0 and mini = -1 reads 'if(brojac[0]>0 && (mini==-1 || brojac[0]

brojac[-1] doesn't exist there for you are references some random point in memory that can contain anything.

The correction is:

maxi=1;
mini=1;

//Nadji INDEX NAJVECEG ELEMENTA HISTOGRAMA
for(i=1;i<=100;i++) {// i was set to zero, it's set to one cause the range the code accepts is between 1 and 100
    if(brojac[i]>brojac[maxi]) {
        maxi=i;
    }
    if(brojac[i]<brojac[mini]) { 
        mini=i;
    }
}
K_M
  • 14
  • 2
  • Since it is defined as `int brojac[100];` your loop is writing out of bounds. `brojac[100]` is not valid. – Osiris Oct 19 '18 at 19:25
  • Also note that due to [short circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation) `brojac[-1]` is never accessed. – Osiris Oct 19 '18 at 19:31
-2
for(i=0;i<=100;i++) -> for(i=0;i<100;i++)
pushkin
  • 9,575
  • 15
  • 51
  • 95
Cluv
  • 27
  • 5
  • 3
    What exactly differs between the two lines? Why would that change make the program work? What happens with the faulty code? Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Some programmer dude Oct 19 '18 at 17:42