Determinare il valore massimo di un vettore [C]
Inviato: sab apr 05, 2014 11:19 pm
http://prntscr.com/37bpin
Il forum italiano su PC, Internet e smartphone
https://turbolab.it/forum/
Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#define n_max 20
int main()
{
float voto[n_max]; //Voto vettore
char i_n[n_max]; //Iniziali nome vettore
char i_c[n_max]; //Iniziali cognome vettore
int pos; //Posizione
int scelta; //Scelta nel case
int num_stud; //Numero studenti
int pos_voto_max; //Posizione voto alto
float voto_max; //Voto massimo
for (pos=0;pos<n_max;pos++) {
voto[pos]=0;
i_n[pos]=' ';
i_c[pos]=' ';
}
printf("Numero studenti: ");
scanf("%d", &num_stud);
while (num_stud>n_max || num_stud<1) {
printf("Errore");
printf("Numero studenti: ");
scanf("%d", &num_stud);
}
for (pos=0;pos<num_stud;pos++) {
printf("\nInserisci voto: ");
scanf("%f", &voto);
printf("Inserisci iniziale nome: ");
scanf(" %c", &i_n);
printf("Inserisci iniziale cognome: ");
scanf(" %c", &i_c);
}
printf("\nInserisci la tua scelta (1-11, 0 per finire): ");
scanf("%d", &scelta);
switch (scelta) {
case 1:
printf("\nVoto, iniziale nome, iniziale cognome del primo studente: %.1f %c %c", voto[0],i_n[0],i_c[0]);
break;
case 2:
printf("\nVoto, inizale nome, iniziale cognome dell'ultimo studente: %.1f %c %c", voto[num_stud-1],i_n[num_stud-1],i_c[num_stud-1]);
break;
case 3:
voto_max=0;
pos_voto_max=0;
for(pos=0;pos<num_stud;pos++) {
if(voto[pos]>voto_max) {
voto_max=voto[pos];
pos_voto_max=pos;
}
}
printf("Il voto massimo e' %.2f e la sua posizione e' %d", voto_max,pos_voto_max);
}
return 0;
}
Codice: Seleziona tutto
for (pos=0;pos<num_stud;pos++) {
printf("\nInserisci voto: ");
scanf("%f", &voto);
printf("Inserisci iniziale nome: ");
scanf(" %c", &i_n);
printf("Inserisci iniziale cognome: ");
scanf(" %c", &i_c);
}
Codice: Seleziona tutto
for (pos = 0; pos<num_stud; pos++) {
printf( "\nInserisci voto: " );
scanf( "%f", &voto[pos] );
printf( "Inserisci iniziale nome: " );
scanf( " %c", &i_n[pos] );
printf( "Inserisci iniziale cognome: " );
scanf( " %c", &i_c[pos] );
}