B
bpascal123
Hi,
I'd like to know how to use well FOR et WHILE. I post this script
below so to illustrate a quicksort program :
1st : it doesn't work
2nd : I 'd like to see how to replace for by while or while by for ---
for that specific program (I couldn't figure this out altough I spent
a lot of time on this - but the programm I'm doing so from is not even
fully working, I made it work once by changing variable POS :
Everything below ( plz don't include functions, for the time i'm
spending to learn, i'd like to deepen my knowledge of basic structures
and arrays...)
===========================================
START HERE
===========================================
main()
{
/* Déclarations */
int A[50]; /* table */
int VAL; /* value */
int POS; /* position */
int N; /* dimension */
int I; /* indice courant */
int INF, MIL, SUP; /* limits */
printf("\n\n===============\n\n") ;
printf("How many numbers in the array ? ") ;
scanf("%d", &N);
for (I=0; I<N; I++)
{
A = op * 10 / 8 + 6 ;
op = A ;
printf("%4d", A) ;
}
printf("\n\n===============\n\n") ;
printf("Enter a number you are looking for : ");
scanf("%d", &VAL );
/* Display the array */
printf("Array entered is : \n");
for (I=0; I<N; I++)
printf("%d ", A);
printf("\n");
/* Set search limits*/
INF=0;
SUP=N-1;
/* Search the number */
POS=-1;
while ((INF<=SUP) && (POS==-1))
{
MIL=(SUP+INF)/2;
if (VAL < A[MIL])
SUP=MIL-1;
else if (VAL > A[MIL])
INF=MIL+1;
else
POS=MIL;
}
/* Print the result of the search */
if (POS==-1)
printf("No match\n");
else
printf("Value is %d and its position %d. \n",
VAL, POS);
return 0;
===========================================
ENDS HERE ( with the last "main" bracket )
===========================================
Below it the same one but fully working (i didn't translate it as
variables remain the same)
===========================================
START HERE
===========================================
#include <stdio.h>
main()
{
/* Déclarations */
int A[50]; /* tableau donné */
int VAL; /* valeur à rechercher */
int POS; /* position de la valeur */
int N; /* dimension */
int I; /* indice courant */
/* Saisie des données */
printf("Dimension du tableau (max.50) : ");
scanf("%d", &N );
for (I=0; I<N; I++)
{
printf("Elément %d : ", I);
scanf("%d", &A);
}
printf("Elément à rechercher : ");
scanf("%d", &VAL );
/* Affichage du tableau */
printf("Tableau donné : \n");
for (I=0; I<N; I++)
printf("%d ", A);
printf("\n");
/* Recherche de la position de la valeur */
POS = -1;
for (I=0 ; (I<N)&&(POS==-1) ; I++)
if (A==VAL)
POS=I;
/* Edition du résultat */
if (POS==-1)
printf("La valeur recherchée ne se trouve pas "
"dans le tableau.\n");
else
printf("La valeur %d se trouve à la position %d. \n",
VAL, POS);
return 0;
===========================================
ENDS HERE ( with the last "main" bracket )
===========================================
Please focus your help just on FOR and WHILE, so far I can't deal with
ANSI or NON ANSI issues for scanf and so on as I'd really like to
first understand such easy loop which I think are essentials.
Many thanks if you can take me out of that :
Pascal
I'd like to know how to use well FOR et WHILE. I post this script
below so to illustrate a quicksort program :
1st : it doesn't work
2nd : I 'd like to see how to replace for by while or while by for ---
for that specific program (I couldn't figure this out altough I spent
a lot of time on this - but the programm I'm doing so from is not even
fully working, I made it work once by changing variable POS :
Everything below ( plz don't include functions, for the time i'm
spending to learn, i'd like to deepen my knowledge of basic structures
and arrays...)
===========================================
START HERE
===========================================
main()
{
/* Déclarations */
int A[50]; /* table */
int VAL; /* value */
int POS; /* position */
int N; /* dimension */
int I; /* indice courant */
int INF, MIL, SUP; /* limits */
printf("\n\n===============\n\n") ;
printf("How many numbers in the array ? ") ;
scanf("%d", &N);
for (I=0; I<N; I++)
{
A = op * 10 / 8 + 6 ;
op = A ;
printf("%4d", A) ;
}
printf("\n\n===============\n\n") ;
printf("Enter a number you are looking for : ");
scanf("%d", &VAL );
/* Display the array */
printf("Array entered is : \n");
for (I=0; I<N; I++)
printf("%d ", A);
printf("\n");
/* Set search limits*/
INF=0;
SUP=N-1;
/* Search the number */
POS=-1;
while ((INF<=SUP) && (POS==-1))
{
MIL=(SUP+INF)/2;
if (VAL < A[MIL])
SUP=MIL-1;
else if (VAL > A[MIL])
INF=MIL+1;
else
POS=MIL;
}
/* Print the result of the search */
if (POS==-1)
printf("No match\n");
else
printf("Value is %d and its position %d. \n",
VAL, POS);
return 0;
===========================================
ENDS HERE ( with the last "main" bracket )
===========================================
Below it the same one but fully working (i didn't translate it as
variables remain the same)
===========================================
START HERE
===========================================
#include <stdio.h>
main()
{
/* Déclarations */
int A[50]; /* tableau donné */
int VAL; /* valeur à rechercher */
int POS; /* position de la valeur */
int N; /* dimension */
int I; /* indice courant */
/* Saisie des données */
printf("Dimension du tableau (max.50) : ");
scanf("%d", &N );
for (I=0; I<N; I++)
{
printf("Elément %d : ", I);
scanf("%d", &A);
}
printf("Elément à rechercher : ");
scanf("%d", &VAL );
/* Affichage du tableau */
printf("Tableau donné : \n");
for (I=0; I<N; I++)
printf("%d ", A);
printf("\n");
/* Recherche de la position de la valeur */
POS = -1;
for (I=0 ; (I<N)&&(POS==-1) ; I++)
if (A==VAL)
POS=I;
/* Edition du résultat */
if (POS==-1)
printf("La valeur recherchée ne se trouve pas "
"dans le tableau.\n");
else
printf("La valeur %d se trouve à la position %d. \n",
VAL, POS);
return 0;
===========================================
ENDS HERE ( with the last "main" bracket )
===========================================
Please focus your help just on FOR and WHILE, so far I can't deal with
ANSI or NON ANSI issues for scanf and so on as I'd really like to
first understand such easy loop which I think are essentials.
Many thanks if you can take me out of that :
Pascal