-1

How can i use read_vector and display_vector in C? I have to shuffle array and then use this functions. void shuffle(int tab[], int size). I don't know if read_vector and display_vector is good. Any help will be good. (It have to be max 100 numebrs)

#include<stdio.h>

void shuffle(int tab[], int size);
int read_vector(int vec[], int size, int stop_value);
void display_vector(const int vec[], int size);

int main()
{
int i; 
int tab[101], a;

printf("Podaj pierwszy wektor: ");
for(i=0; i<100; i++)
{
a = scanf("%d", &tab[i]);
if(a<1)
{
    printf("Incorrect input");
    return 1;
}
if(tab[0]==0)
{
    printf("Not enough data available");
    return 2;
}
if(tab[i]==0)
{
    break;
}
}

shuffle(tab[i], i);

for(i=0; i<100; i++)
{
    printf("%d", tab[i]);
}
return 0;
}

void shuffle(int tab[], int size)
{
int i, j=0, x=0;

for(i=size; i>0; i--)
{
    j = rand() % size+1;
    x = tab[i];
    tab[i]=tab[j];
    tab[j]=x;
    x=0;
}

}
Mangesh Pawar
  • 309
  • 1
  • 14
  • 2
    I missed the *specific* question in your question. If the code works, this should be on [codereview.stackexchange.com](http://codereview.stackexchange.com). If it doesn't, what *specifically* seems to be wrong? How is the behavior you're expecting different than the behavior you're actually *getting*? This seems an *ideal* candidate for a debugger if it indeed is malfunctioning. – WhozCraig Jan 02 '19 at 04:11
  • I dont know how to return shuffle. How to use read_vector, display_vector. It should loading and displaying. I never did that so i need help with this. – HatariiMisaka Jan 02 '19 at 04:14
  • 1
    for ease of readability and understanding: 1) use meaningful variable names. variable names should indicate `content` or `usage` (or better, both) 2) please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces – user3629249 Jan 02 '19 at 04:15
  • regarding: `j = rand() % size+1;` before calling `rand()` the function `srand()` needs to be called (exactly) once. Typically very near the top of `main()` with a statement like: `srand( (unsigned)time( void ) );` note: for success, the header file: `time.h` should be one of the header files that are included – user3629249 Jan 02 '19 at 04:17
  • regarding: `if(a<1) { printf("Incorrect input"); return 1; }` 1) error messages should be output to `stderr`, not `stdout`. 2) the `return 1` statement is very risky, Suggest: `exit( EXIT_FAILURE ); How to output to `stderr`? suggest: `fprintf( stderr, "scanf failed\n" );` – user3629249 Jan 02 '19 at 04:20
  • regarding: `shuffle(tab[i], i);` this is passing the first argument as 1) an integer rather than a pointer to the array `tab[]` 2) actually is passing an item beyond the end of the initialized table elements. Suggest: `shuffle(tab, i);` – user3629249 Jan 02 '19 at 04:28
  • Okey but how can i add it to int main() And print shuffle numbers? – HatariiMisaka Jan 02 '19 at 04:29
  • the function: `rand()` (and my suggested function: `srand()` are exposed in the header file: `stdlib.h` So the code needs to have the statement: `#include ` inserted at the beginning of the code – user3629249 Jan 02 '19 at 04:31
  • *Okey but how can i add it to int main() And print shuffle numbers?* Ok to what comment? – user3629249 Jan 02 '19 at 04:32
  • the posted code fails to inform the user that entering a 0 causes the code to quit asking for more numbers. Especially when the user enters a 0, then this: `if(a<1) { printf("Incorrect input"); return 1; }` will cause the program to exit – user3629249 Jan 02 '19 at 04:34
  • I dont know how to use void shuffle(int tab[], int size) which i did. Every time i did return something but now i dont know what to return in the end. Maybe i have to use read_vector and display_vector but i dont know how. – HatariiMisaka Jan 02 '19 at 04:35

1 Answers1

0

the following proposed code:

  1. cleanly compiles
  2. incorporates the comments to the question
  3. performs the desired functionality
  4. properly checks for errors
  5. eliminated the 'magic' numbers 100 and 101
  6. since the OPs code does not have separate functions for read_vector() nor display_vector() I did not implement them separately, However, they should be trivial to implement

And now the proposed code:

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

void shuffle(int tab[], int size);
//int read_vector(int vec[], int size, int stop_value);
//void display_vector(const int vec[], int size);

#define MAX_VECTOR_LENGTH 100

int main( void )
{
    int i; 
    int tab[ MAX_VECTOR_LENGTH ];

    srand( (unsigned)time( NULL ) );

    printf("Podaj pierwszy wektor:\n ");
    for(i=0; i<MAX_VECTOR_LENGTH; i++)
    {
        printf( "%s", "enter vector entry or 0 to indicate done " );
        if( scanf("%d", &tab[i]) != 1)
        {
            fprintf( stderr, "scanf failed\n");
            exit( EXIT_FAILURE );
        }

        // implied else, scanf successful

        if(tab[i]==0)
        {
            break;
        }
    }

    shuffle(tab, i-1);

    for(int j = 0; j < i; j++)
    {
        printf("%d  ", tab[j]);
    }
    return 0;
}


void shuffle(int tab[], int size)
{
    int i;
    int j;
    int x;

    for(i=size; i>0; i--)
    {
        j = rand() % size;
        x = tab[i];
        tab[i]=tab[j];
        tab[j]=x;
    }
}

A typical run of the program results in:

Podaj pierwszy wektor: 
enter vector entry or 0 to indicate done 1
enter vector entry or 0 to indicate done 2
enter vector entry or 0 to indicate done 3
enter vector entry or 0 to indicate done 4
enter vector entry or 0 to indicate done 5
enter vector entry or 0 to indicate done 6
enter vector entry or 0 to indicate done 7
enter vector entry or 0 to indicate done 0
1  6  5  3  4  7  2  
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • notice that I did not handle the corner cases of 0 or 1 entries in the `tab[]` array – user3629249 Jan 02 '19 at 05:06
  • notice that if there are an even number of items in the array that it is possible for the resulting array to be unchanged after the call to `shuffle()` – user3629249 Jan 02 '19 at 05:09
  • Your shuffle (and OPs) is biased. See https://stackoverflow.com/questions/859253/why-does-this-simple-shuffle-algorithm-produce-biased-results-what-is-a-simple (or many other pages, that was just the first one which showed up in a search). – rici Jan 02 '19 at 13:36
  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern – user3629249 Jan 02 '19 at 15:52