C functions shuffle, read_vector, display_vector












-2















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;
}

}









share|improve this question




















  • 2





    I missed the specific question in your question. If the code works, this should be on 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 2 at 4: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 2 at 4: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 2 at 4: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 2 at 4: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 failedn" );`

    – user3629249
    Jan 2 at 4:20


















-2















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;
}

}









share|improve this question




















  • 2





    I missed the specific question in your question. If the code works, this should be on 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 2 at 4: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 2 at 4: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 2 at 4: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 2 at 4: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 failedn" );`

    – user3629249
    Jan 2 at 4:20
















-2












-2








-2








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;
}

}









share|improve this question
















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;
}

}






c function vector shuffle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 12:09









Mangesh Pawar

6310




6310










asked Jan 2 at 3:58









HatariiMisakaHatariiMisaka

11




11








  • 2





    I missed the specific question in your question. If the code works, this should be on 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 2 at 4: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 2 at 4: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 2 at 4: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 2 at 4: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 failedn" );`

    – user3629249
    Jan 2 at 4:20
















  • 2





    I missed the specific question in your question. If the code works, this should be on 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 2 at 4: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 2 at 4: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 2 at 4: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 2 at 4: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 failedn" );`

    – user3629249
    Jan 2 at 4:20










2




2





I missed the specific question in your question. If the code works, this should be on 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 2 at 4:11







I missed the specific question in your question. If the code works, this should be on 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 2 at 4: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 2 at 4:14





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 2 at 4:14




1




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 2 at 4:15





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 2 at 4: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 2 at 4:17





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 2 at 4: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 failedn" );`

– user3629249
Jan 2 at 4:20







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 failedn" );`

– user3629249
Jan 2 at 4:20














1 Answer
1






active

oldest

votes


















-1














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 failedn");
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





share|improve this answer


























  • notice that I did not handle the corner cases of 0 or 1 entries in the tab array

    – user3629249
    Jan 2 at 5: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 2 at 5:09











  • Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

    – rici
    Jan 2 at 13:36











  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

    – user3629249
    Jan 2 at 15:52











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001040%2fc-functions-shuffle-read-vector-display-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














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 failedn");
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





share|improve this answer


























  • notice that I did not handle the corner cases of 0 or 1 entries in the tab array

    – user3629249
    Jan 2 at 5: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 2 at 5:09











  • Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

    – rici
    Jan 2 at 13:36











  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

    – user3629249
    Jan 2 at 15:52
















-1














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 failedn");
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





share|improve this answer


























  • notice that I did not handle the corner cases of 0 or 1 entries in the tab array

    – user3629249
    Jan 2 at 5: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 2 at 5:09











  • Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

    – rici
    Jan 2 at 13:36











  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

    – user3629249
    Jan 2 at 15:52














-1












-1








-1







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 failedn");
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





share|improve this answer















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 failedn");
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






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 5:03

























answered Jan 2 at 4:58









user3629249user3629249

11.2k11014




11.2k11014













  • notice that I did not handle the corner cases of 0 or 1 entries in the tab array

    – user3629249
    Jan 2 at 5: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 2 at 5:09











  • Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

    – rici
    Jan 2 at 13:36











  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

    – user3629249
    Jan 2 at 15:52



















  • notice that I did not handle the corner cases of 0 or 1 entries in the tab array

    – user3629249
    Jan 2 at 5: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 2 at 5:09











  • Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

    – rici
    Jan 2 at 13:36











  • Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

    – user3629249
    Jan 2 at 15:52

















notice that I did not handle the corner cases of 0 or 1 entries in the tab array

– user3629249
Jan 2 at 5:06





notice that I did not handle the corner cases of 0 or 1 entries in the tab array

– user3629249
Jan 2 at 5: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 2 at 5:09





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 2 at 5:09













Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

– rici
Jan 2 at 13:36





Your shuffle (and OPs) is biased. See stackoverflow.com/questions/859253/… (or many other pages, that was just the first one which showed up in a search).

– rici
Jan 2 at 13:36













Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

– user3629249
Jan 2 at 15:52





Nothing in the question indicated a desire for an unbiased shuffle. So that is not a concern

– user3629249
Jan 2 at 15:52




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001040%2fc-functions-shuffle-read-vector-display-vector%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'