Thread: Compare user input and function with array and bubblesort

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Compare user input and function with array and bubblesort

    Hi

    I have a function to create a random array with 4 numbers

    Code:
    void vetorOrdenado()
    {
    	int num[TAM];
    		int i;
    		printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Numeros random<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
    		LeVetor(num);
    		// Iteração através do array. Os números já estão random
    		for(i = 0;i <4;++i)
    			printf("%d\n", num[i]);
    		printf("\n");
    		bubbleSort(num, 4);
    Now i want to create a function to after the output the numbers, the user must sort the array then compare one array to the other.

    Right now i have this.

    Code:
    int jogo1Gp3()
    {
        int vet[3], i;
        system("cls");
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Ordenacao rapida<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        vetorOrdenado_jogo();
        printf("\nSort those numbers by the smalest to the highest\n");
        for (i=0; i<=3; i++)
        {
            printf("Sort those numbers ");
            scanf("%d", &vet[i]);
        }
        if(vet[i]==vetorOrdenador(num,4))
        return 1;
        else
        return 0;
    
    
    }
    Is this correct??

    Thnaks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > for (i=0; i<=3; i++)
    Using <= in a loop, which starts at 0, AND is accessing an array, is almost always a sign of an array overrun.

    Nevermind that you also use i AFTER the loop has finished (which would be an array overrun even if you got the loop right).

    > if(vet[i]==vetorOrdenador(num,4))
    Do any of your actual functions have prototypes?
    I mean, all you have is
    void vetorOrdenado()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi Salem,

    Yes i have, see:

    Code:
    void LeVetor(int num[])
    {
    	int i, n, tmp;
    	srand(time(NULL));
    	// Inicia o array
    	for(i = 0;i < TAM;++i)
    		num[i] = i;
    	for (i = TAM; i > 1;)
    	{
    		n = rand() % i;
    		--i;
    		tmp = num[n];
    		num[n] = num[i];
    		num[i] = tmp;
    	}
    	for(i = 0;i < 4;++i)
    		printf("%d\n", num[i]);
    }
    Code:
    void bubbleSort(int v[], int n)
    {
      int i, j, temp;
      for (i = n; i > 0; i--)
        for (j = 1; j < i; j++)
          if(v[j-1] > v[j])
          {
              temp = v[j-1];
              v[j-1] = v[j];
              v[j] = temp;
              }
    }
    Then the last one:

    Code:
    void vetorOrdenado()
    {
    	int num[TAM];
    		int i;
    		printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Numeros random<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
    		LeVetor(num);
    		// Iteração através do array. Os números já estão random
    		for(i = 0;i <4;++i)
    			printf("%d\n", num[i]);
    		printf("\n");
    		bubbleSort(num, 4);
    }

    It is correct?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Maybe, replace 4 with TAM in all appropriate places.

    > void bubbleSort(int v[], int n)
    Good interface.

    > void LeVetor(int num[])
    How about making this
    void LeVetor(int num[], int n)
    as well?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    If i put
    void LeVetor(int num[], int n)

    error: 'n' redeclared as different kind of symbol

    I have already variable n declared inside that fucntion (can i use another one inside?)

    I can't put TAM in all because TAM is 100, and i want to have 4 numbers between 0 and 100 and then, the user must sort those 4 numbers in few seconds.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I have already variable n declared inside that fucntion (can i use another one inside?)
    Yes. Just be sure the variable declared in the argument list of the function and the variable inside of the function body have different names.

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Ok

    Now i have all 3 functions like this

    Code:
    void LeVetor(int num[], int n)
    {
    	int i, x, tmp;
    	srand(time(NULL));
    	// Inicia o array
    	for(i = 0;i < TAM;++i)
    		num[i] = i;
    	// Embaralha o array
    	for (i = TAM; i > 1;)
    	{
    		x = rand() % i;
    		--i;
    		tmp = num[x];
    		num[x] = num[i];
    		num[i] = tmp;
    	}
    	for(i = 0;i < 4;++i)
    		printf("%d\n", num[i]);
    }
    void bubbleSort(int v[], int n)
    {
      int i, j, temp;
      for (i = n; i > 0; i--)
        for (j = 1; j < i; j++)
          if(v[j-1] > v[j])
          {
              temp = v[j-1];
              v[j-1] = v[j];
              v[j] = temp;
              }
    }
    
    
    void vetorOrdenado()
    {
    	    int num[TAM];
    		int i;
    		int n=4;
    		printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Numeros random<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
    		LeVetor(num, n);
    		// Iteração através do array. Os números já estão random
    		for(i = 0;i <4;++i)
    			printf("%d\n", num[i]);
    		printf("\n");
    		bubbleSort(num, 4);
    		printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Vetor ordenado<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
            for(i=0; i<4; i++)
    			{
    			    printf("%d\n",num[i]);
    			}
    }
    Now i have that function who must give me 4 numbers (gonna call leVetor())

    Then the user will be asked to sort those numbers

    Finally i must compare with vetorOrdenado() the two arrays and return 1 or 0

    So what i have so far is

    Code:
    int jogo1Gp3(int num[], int n)
    {
        int vet[3], i;
        system("cls");
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Ordenacao rapida<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        leVetor();
        printf("\nOrdene os 4 primeiros numeros de forma ascendente\n");
        for (i=0; i<=3; i++)
        {
            printf("Ordene estes numeros ");
            scanf("%d", &vet[i]);
        }
        if(vet[i]==vetorOrdenado_jogo(num,n))
        printf("They are equal");
    }
    I have this error

    error: void value not ignored as it ought to be|
    warning: zero-length ms_printf format string [-Wformat-zero-length]|

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if(vet[i]==vetorOrdenado_jogo(num,n))
    I see a function definition for "vetorOrdenado" but not "vetorOrdenado_jogo". If this function is declared as not having a return type ("void vetorOrdenado_jogo(...)"), then it has no value and cannot be used for the comparison ("==").

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I think you missed the whole point of passing an array length to this function.
    Code:
    void LeVetor(int num[], int n)
    {
    	int i, x, tmp;
    	srand(time(NULL));
    	// Inicia o array
    	for(i = 0;i < TAM;++i) //!! why TAM, and not n?
    		num[i] = i;
    	// Embaralha o array
    	for (i = TAM; i > 1;)  //!! why TAM, and not n?
    	{
    		x = rand() % i;
    		--i;
    		tmp = num[x];
    		num[x] = num[i];
    		num[i] = tmp;
    	}
    	for(i = 0;i < 4;++i)  //!! why 4, and not n?
    		printf("%d\n", num[i]);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Sorry Salem, you are right, didn't changed 4 to n, already made the changes.

    Quote Originally Posted by Matticus View Post
    Code:
    if(vet[i]==vetorOrdenado_jogo(num,n))
    I see a function definition for "vetorOrdenado" but not "vetorOrdenado_jogo". If this function is declared as not having a return type ("void vetorOrdenado_jogo(...)"), then it has no value and cannot be used for the comparison ("==").
    Yes i see that my function is void and this one is int, so i must change all to int, right?

    After that i can use return , right?
    Last edited by Gil Carvalho; 06-15-2012 at 12:07 PM. Reason: error

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Ok, i think i get it;

    Code:
    int jogo1Gp3(int num[], int n)
    {
        int vet[3], i;
        system("cls");
            printf ("\n*=============================================================================*\n");
            printf ("|                       >>>>Ordenacao rapida<<<<                                |\n");
            printf ("*=============================================================================*\n");
            printf ("                                                                               \n");
        LeVetor(num,n);
        printf("\nOrdene os 4 primeiros numeros de forma ascendente\n");
        for (i=0; i<=3; i++)
        {
            printf("Ordene estes numeros ");
            scanf("%d", &vet[i]);
        }
        if(vet[i]==vetorOrdenado(num,n))
        return 1;
        else
        return 0;
    }
    Now to call that function i need

    jogo1Gp3(argument?)

  12. #12
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Now to call that function i need jogo1Gp3(argument?)

    Yes, you do need an argument. 2, to be exact.

    But before you go and post again, why don't you actually do a little research? It seems like all of your threads turn into gigantic spoon-feeding sessions that further nothing but the immediate solution of a minor problem. And that's wasting everyones time. We don't like to sit here and give you answers, and you've been working on that freakin' "jogo" project for months.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by memcpy View Post
    > Now to call that function i need jogo1Gp3(argument?)

    Yes, you do need an argument. 2, to be exact.

    But before you go and post again, why don't you actually do a little research? It seems like all of your threads turn into gigantic spoon-feeding sessions that further nothing but the immediate solution of a minor problem. And that's wasting everyones time. We don't like to sit here and give you answers, and you've been working on that freakin' "jogo" project for months.

    Well, first let me say thanks for your help.

    About that freakin' like you call him, let me explain you 2 simple things.
    1- That freakin' game is what i have to do to be aproved to make a final exam. I'm working all day, i'm divorced, i have to pay 2 houses because i'm working at 1500 miles from my town, i have to pay food pension to my daughter...and my university is 1400 miles from me. So excuse me if that freakin' game like you say is taking months.

    2- I have to learn all by myself and believe me is taking me a lot of effort since i don't have too much time to do it. When i'm asking something is because i don't understand how to get to a point, it's not because i want to have my work done. If you don't know i will have to "defend" what i'm doing with that freakin' game.

    Since all of you are better than me, i just want to understand how this langauge works.

    I hope now that you have a different image from me.

    Best regards

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    I think you've somewhat misinterpreted memcpy's statements. One of their comments was about doing some research before asking questions.

    For instance, in post #7, you said you had:

    error: void value not ignored as it ought to be
    Copy + paste that into Google, and the first hit pulls up a page that explains:

    It means a function is declared to return nothing (void) but you are assigning the return code to a variable. eg,

    Code:
    void foo() {
       // do stuff
    }
    
    int main() {
       int x = foo();  // error, foo returns nothing
    }
    That should have been able to help you find where the error came from.

    Programming is only partially about writing code - it's also about problem solving and troubleshooting (among other things). These are the skills required to become a proficient programmer.

  15. #15
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi Matticus, i did that research

    Void value not ignored as it ought to be, in C? - Yahoo! Answers

    [SOLVED] "error:void value not ignored as it ought to be"

    objective c - sortUsingFunction and "void value not ignored as it ought to be" - Stack Overflow

    I have a lot of more like this, if i'm asking something is because i didn't find what i'm looking for

    About missinterpreted memcpy i just said that i have a difficult life (like all of us) and it's not easy to me work, be at university (without assisting to any class) and have to study and apply all this alone. I wasn't "attack" anyone, but it seems that somes have a wrong idea about me.

    *I did all the suggestions and now i have a segmentation fault here

    Code:
    int LeVetor(int num[], int n)
    {
        int i, x, tmp;
        srand(time(NULL));
        // Inicia o array
        for(i = 0;i < TAM;++i)
            num[i] = i;
    int gp3_Ques(char *usr1, int *result, int num[], int n)
    if(jogo1Gp3(num,n))

    It's wrong??
    Last edited by Gil Carvalho; 06-15-2012 at 01:14 PM. Reason: error

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  2. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  3. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  4. How do I allow a user to input a function?
    By bananasinpajama in forum C++ Programming
    Replies: 9
    Last Post: 05-19-2005, 11:11 AM
  5. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM