20/01/19 13:16
Rans
caricare in due vettori i nomi e i punteggi di n concorrenti di una gara.
stampare l'elenco dei concorrenti in ordine alfabetico.
Visualizzare i primi tre classificati.
(per l'ordinamento usare i sottoprogrammi)
Sono riuscito a risolvere il problema ma non riesco a svolgerlo usando un sottoprogramma per l'ordinamento
// soluzione senza sottoprogramma funzionante
#include <stdio.h>
#include <string.h>
main(){
system("title S03"
system("color f0"
srand(time(NULL));
int n,pun[10],temp,i,j;
char nome[10][20], temp2[20];
do{
printf("\nInserire num. giocatori: "
scanf("%d",&n);
}while(n<1 || n>10);
for(i=0;i<n;i++){
printf("\nNome: "
scanf("%s",nome[i]);
pun[i]=(rand()%100)+1;
printf("%d",pun[i]);
}
for(j=0;j<n-1;j++){
for(i=0;i<n-1;i++){
if(nome[i][0]>nome[i+1][0]){
temp=pun[i];
pun[i]=pun[i+1];
pun[i+1]=temp;
strcpy(temp2, nome[i]);
strcpy(nome[i], nome[i+1]);
strcpy(nome[i+1], temp2);
}
}
}
for(i=0;i<3;i++)
printf("\n%s : %d",nome[i],pun[i]);
printf("\n\n"
system("pause"
}
//soluzione con sottoprogramma non funzionante
#include <stdio.h>
#include <string.h>
void ord(char a, char b);
int main(){
system("title S03"
system("color f0"
srand(time(NULL));
int n,pun[10],temp,i,j;
char nome[10][20], temp2[20];
do{
printf("\nInserire num. giocatori: "
scanf("%d",&n);
}while(n<1 || n>10);
for(i=0;i<n;i++){
printf("\nNome: "
scanf("%s",nome[i]);
pun[i]=(rand()%100)+1;
printf("%d",pun[i]);
}
for(i=0;i<n;i++)
ord(nome[i][0],nome[i+1][0]);
for(i=0;i<n;i++)
printf("\n%s : %d",nome[i],pun[i]);
printf("\n\n"
system("pause"
}
// ordinamento nomi in ordine alfabetico
void ord(char a, char b)
{
int j, i, n, temp, pun[10];
char temp2[20];
for(j=0;j<n-1;j++){
for(i=0;i<n-1;i++){
if(a>b){
temp=pun[i];
pun[i]=pun[i+1];
pun[i+1]=temp;
strcpy(temp2, a);
strcpy(a, b);
strcpy(b, temp2);
}
}
}
}
ecco gli errori:
F:\S03.c In function 'ord':
39 19 F:\S03.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char'
40 12 F:\S03.c [Warning] passing argument 1 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char'
40 15 F:\S03.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char'
41 12 F:\S03.c [Warning] passing argument 1 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char'
stampare l'elenco dei concorrenti in ordine alfabetico.
Visualizzare i primi tre classificati.
(per l'ordinamento usare i sottoprogrammi)
Sono riuscito a risolvere il problema ma non riesco a svolgerlo usando un sottoprogramma per l'ordinamento
// soluzione senza sottoprogramma funzionante
#include <stdio.h>
#include <string.h>
main(){
system("title S03"
system("color f0"
srand(time(NULL));
int n,pun[10],temp,i,j;
char nome[10][20], temp2[20];
do{
printf("\nInserire num. giocatori: "
scanf("%d",&n);
}while(n<1 || n>10);
for(i=0;i<n;i++){
printf("\nNome: "
scanf("%s",nome[i]);
pun[i]=(rand()%100)+1;
printf("%d",pun[i]);
}
for(j=0;j<n-1;j++){
for(i=0;i<n-1;i++){
if(nome[i][0]>nome[i+1][0]){
temp=pun[i];
pun[i]=pun[i+1];
pun[i+1]=temp;
strcpy(temp2, nome[i]);
strcpy(nome[i], nome[i+1]);
strcpy(nome[i+1], temp2);
}
}
}
for(i=0;i<3;i++)
printf("\n%s : %d",nome[i],pun[i]);
printf("\n\n"
system("pause"
}
//soluzione con sottoprogramma non funzionante
#include <stdio.h>
#include <string.h>
void ord(char a, char b);
int main(){
system("title S03"
system("color f0"
srand(time(NULL));
int n,pun[10],temp,i,j;
char nome[10][20], temp2[20];
do{
printf("\nInserire num. giocatori: "
scanf("%d",&n);
}while(n<1 || n>10);
for(i=0;i<n;i++){
printf("\nNome: "
scanf("%s",nome[i]);
pun[i]=(rand()%100)+1;
printf("%d",pun[i]);
}
for(i=0;i<n;i++)
ord(nome[i][0],nome[i+1][0]);
for(i=0;i<n;i++)
printf("\n%s : %d",nome[i],pun[i]);
printf("\n\n"
system("pause"
}
// ordinamento nomi in ordine alfabetico
void ord(char a, char b)
{
int j, i, n, temp, pun[10];
char temp2[20];
for(j=0;j<n-1;j++){
for(i=0;i<n-1;i++){
if(a>b){
temp=pun[i];
pun[i]=pun[i+1];
pun[i+1]=temp;
strcpy(temp2, a);
strcpy(a, b);
strcpy(b, temp2);
}
}
}
}
ecco gli errori:
F:\S03.c In function 'ord':
39 19 F:\S03.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char'
40 12 F:\S03.c [Warning] passing argument 1 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char'
40 15 F:\S03.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'const char * __restrict__' but argument is of type 'char'
41 12 F:\S03.c [Warning] passing argument 1 of 'strcpy' makes pointer from integer without a cast
3 0 F:\S03.c In file included from F:\S03.c
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note] expected 'char * __restrict__' but argument is of type 'char'
aaa