0

I'm doing an exercise in C, but I don't know why as first result I have always -1 (that is impossible). I have -1 only after the swap to ordinate the array.

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

main(){
        int vet[100], cont[100];
        int i, c, f=100;
        int swap;
        int r=0;
        int search;

        srand(time(NULL));

        for(i=0;i<100;i++){
                vet[i]=rand()%100+1;
        }

        while(r==0){
                r=1;
                for(i=0;i<100;i++){
                        if(vet[i]>vet[i+1]){
                                swap=vet[i+1];
                                vet[i+1]=vet[i];
                                vet[i]=swap;
                                r=0;
                        }
                }
        }

        for(i=0;i<100;i++){
                printf("%d) %d\n", i+1, vet[i]);
        }

        i=0;
        r=0;
        printf("Inserisci numero da ricercare (1-10000) -> ");
        scanf("%d", &search);
        if(search>10000 || search<0){
                printf("Hai inserito un valore non valido\n");
        }
        else{
                c=(i+f)/2;
                while(vet[c]!=search && i<f){
                        if(vet[c]<search){
                                i=c+1;
                                c=(i+f)/2;
                        }
                        else if(vet[c]>search){
                                f=c-1;
                                c=(i+f)/2;
                        }

                        if(vet[c]==search){
                                cont[r]=c+1;
                                r++;
                        }
                }
                if(vet[c]!=search){
                        printf("Non e\' stato trovato nessun valore %d", search◆
                }
                else{
                        for(i=0;i<r;i++){
                                printf("%d\n", cont[i]);
                        }
                }
        }
}

Now I must use srand(time(NULL)) I know that there are better solutions. The exercise isn't complete, now I' m trying to solve this error, someone can help me?

EDIT: I'm using OPENVMS to compile, link and run

Laurent Parenteau
  • 2,516
  • 20
  • 31
Mitro
  • 1,230
  • 8
  • 32
  • 61
  • You don't seem to be sorting the array. –  Jan 11 '13 at 16:07
  • 1
    @H2CO3 he does a kind of a single iteration of bubble sort in the begging. Of course this is not enough – Ivaylo Strandjev Jan 11 '13 at 16:08
  • @izomorphius Yes, I don't consider one iteration sorting for a `k` length array. –  Jan 11 '13 at 16:09
  • 1
    Yes I use bubble sort, why isn't enough? If I put another for before the bubblesort I havent' dispalyed any -1 – Mitro Jan 11 '13 at 16:09
  • 3
    @AlessioMTX no you don't perform a bubble sort. What you have written is but a single iteration out of the needed k(in the worst case) – Ivaylo Strandjev Jan 11 '13 at 16:11
  • Also, `while(temp == 1) { etc.; temp = 0; }` is hard to read, use `break;` instead. –  Jan 11 '13 at 16:13
  • 1
    Ok maybe I've undestood, so when I arrive to if(vet[100]>vet[101]) is overflow?? – Mitro Jan 11 '13 at 16:20
  • 1
    Believe it or not, that code does correctly sort `vet`. Try running it. – aschepler Jan 11 '13 at 16:21
  • 2
    @H2CO3 - I must be missing where the array isn't sorted. We're talking about `vet[]` right? The line starting `while(r==0){` sorts the array. He sets `r=1` right away, but then sets `r=0` again if a sort was required. – Mike Jan 11 '13 at 16:21
  • @izomorphius it is proper sort. Look at `r=0;` which flags the entire iteration to run once again. And again. Until it's sorted. http://codepad.org/1NJjDFYw – Agent_L Jan 11 '13 at 16:23
  • @AlessioMTX: just one thought: when trying to pinpoint a bug, don't use `srand(time)`. Use some constant number to have repeatable `rand` results. – Agent_L Jan 11 '13 at 16:25

1 Answers1

6

Your problem is probably here:

for(i=0;i<100;i++){
    if(vet[i]>vet[i+1]){

When i=99, and you access vet[i+1], you are off the end of the array. This element is not defined, and it's probably just fluke that you don't get any worse behaviour.

EDIT: So the solution is to change in

for(i=0;i<99;i++){
        if(vet[i]>vet[i+1]){
Mitro
  • 1,230
  • 8
  • 32
  • 61
nrussell
  • 358
  • 1
  • 7