All pastes #884849 Raw Edit

Stuff

public text v1 · immutable
#884849 ·published 2008-01-30 17:06 UTC
rendered paste body
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
#define DIM 10

typedef char string[MAXLEN];

void array_put(string array[], int dim, int pos, string value){
	
	int i;
	string temp;
	/*shift degli elementi, il penultimo nell'ultimo e cos via...*/
	for(i=dim-1;i>=pos;i--){
		strcpy( array[i] , array[i-1] );
	}
	/*Inseriamo  l'emementi alla posizione che ci serve*/
	strcpy( array[pos-1] , value );

}

int main(){

	string array[DIM],temp;
	int i,j;
	
	
	for(i=0;i<DIM;i++){
	
		printf("Inserisci il cognome %d: ", i+1);
		scanf("%s",&temp);
		
		/*Scorriamo per  individuare a che posizione va inserito il nuovo cognome*/
		for( j=0; j<i; j++){
		
			if( strcmp( temp , array[j] ) < 0 )
				break;
		}
		/*Se j=i vuol dire che va messo per ultimo*/
		if(j==i)
			 
			  strcpy( array[i] ,temp );
		else
			  /*Altrimenti richiamiamo la funzione che lo inserisce alla posizione desiderata  */
			  array_put( array, DIM , j+1 , temp);
	}
	
	for( i=0; i<DIM; i++)
		printf("Cognome %d: %s\n", i+1 , array[i] );
		
	return 0;
}