1

It tells me that there was an error. For me it's a problem of pointer or something about the structure, but I don't know exactly.

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

typedef struct
{
    char luogo[20];
    float valore;
}rilevazione;

void BubbleSort(float valore[], int n);

int main()
{
int i=0, n=0, j=0;
rilevazione*dato=NULL;

dato=(rilevazione*)malloc(MAX*sizeof(rilevazione));

while(i<MAX)
{
    printf("inserisci luogo %d: ", i+1);
    scanf("%s", dato[i].luogo);
    printf("inserisci valore %d: ", i+1);
    scanf("%f", &dato[i].valore);

    if(strcmp(dato[i].luogo, "end")==0) break;
    i++;
}

n=i;
printf("il numero di misure e' %d", n);
scanf("%d", &n);

BubbleSort(dato.valore, n);
for(j=0; j<n; j++)
{
    printf("valore: %d  luogo: %s", dato[i].valore, dato[i].luogo);
}





return 0;
}

void BubbleSort(float valore[], int n)
{
int i=0, temp=0, j=0;
int scambi=1;

while(i<=n && scambi!=0)
{
  scambi=0;
  j=n;
  while(j>=i)
  {
      if(valore[j]>valore[j+1])
      {
          temp=valore[j];
          valore[j]=valore[j+1];
          valore[j+1]=temp;
          scambi=1;
      }

      j=j-1;
  }
 i=i+1;
}
return;
}
Kninnug
  • 7,992
  • 1
  • 30
  • 42

4 Answers4

0

Your BubbleSort takes an array of floats but you are are trying to pass it just one float and not correctly either. dato.valore has no meaning. dato[n].valore does. Do you see the difference?

Choices:

  1. create an array of floats from your array of structs.
  2. Update BubbleSort to sort the struct array you already have.
  3. Update your sort routine to take a function pointer and calls the function for each item in the list to be sorted.

The last choice is how most libraries deal with this.

Sean Perry
  • 3,776
  • 1
  • 19
  • 31
  • @user3092558, pick a choice from my list and post some updated code. – Sean Perry Dec 11 '13 at 20:15
  • i change but it's the same problem BubbleSort(dato[i].valore, n); – user3092558 Dec 11 '13 at 20:18
  • @user3092558, dato[i].valore means the i-th item in the array's data member valore. Do you see how asking to sort one entry will not work? You are doing the equivalent of `BubbleSort(3.14, n);` – Sean Perry Dec 11 '13 at 20:20
  • Welcome to programming @user3092558. It can be overwhelming at times. The trick is to come up with small steps that lead to victory. Ask yourself, if this code was working 100% what should `BubbleSort` take as input and return as output. Ok, what is the step right before working where `BubbleSort` is almost working but your are missing one piece? Keep walking backwards and eventually you will end up at the right place. – Sean Perry Dec 11 '13 at 20:27
  • bubbleSort take as input the struct...and as output it takes me an error....i don't understand if the problem is in void BubbleSort(float valore[], int n) or in BubbleSort(dato.valore, n); – user3092558 Dec 11 '13 at 20:30
0

That's probably because when you do BubbleSort(dato.valore, n); you are missing an index in dato... which is a pointer.

So you would need something like dato[XXX].valore.

luis
  • 11
  • 1
0
rilevazione*dato=NULL;

dato=(rilevazione*)malloc(MAX*sizeof(rilevazione));

change to

rilevazione *dato;

dato=(rilevazione*)malloc(MAX*sizeof(rilevazione));
if (dato == NULL) { error in malloc'ing data array...

Always check the result of a memory allocation. I'm not sure if C will recognize a pointer declaration when it's all jammed together -- better be safe and put a space in there.

Since dato is an array (actually, a pointer to an array), I suspect that your call to BubbleSort() is not happy with treating it as a scalar structure. You're going to have to pass the entire array of structures into BubbleSort and pick apart the valore component inside the function.

I'll assume that the use of a Bubble Sort is merely incidental and that for real production code you'd use something a little faster running.

Phil Perry
  • 2,126
  • 14
  • 18
0

That's probably because when you do BubbleSort(dato.valore, n); you are missing an index in dato... which is a pointer, so it needs an index.

So you would need something like dato[index].valore.

luis
  • 11
  • 1
  • Sorry, I wanted to edit, but mistakenly put a new answer. [new stackoverflow user...] – luis Dec 11 '13 at 19:32
  • No, putting an index in it would simply pass one structure or one element of one structure, which makes no sense to sort. You need to pass the entire array to a sorting function, and handle the complexity of each element being a structure and not a scalar. – Phil Perry Dec 11 '13 at 21:27