avatar
0033

Guest 1.2K 7th Jan, 2020

#include<stdio.h> // chi can input output thoi
#include<limits.h>

int getInt(int min, int max, char msg[],  char err[]){
	int num;
	int check;
	char c;

	do{
		printf("%s",msg);
	    check = scanf("%d%c", &num, &c);
	    if(check!=2||c!='\n'|| num<min||num>max){
		check =0;
		fflush(stdin); //fpurge(stdin)
		printf("%s",err);
	    }
	}
	while(check==0);
	return num;
}

int main(){
    int n; // size cua mang
    while(1){

	    printf("Dynamic memory allocation\n");
	    n=getInt(1, INT_MAX, "\n\tNumber of element s= ", "Ban can nhap 1 so nguyen duong.");

	    int a[n]; // thuong la a[100] nhung de ko phu hojp vif co the thua hay thieu nen dung a[n]
	    int sum=0;
	    for(int i=0; i<n; i++){
	        a[i]=getInt(INT_MIN, INT_MAX, "\tEnter an integr: ", "Ban can nhap 1 so nguyen duong.\n");
	    	    sum+=a[i];
        }


	    printf("\tSum = %d ", sum);
	    printf("\nPress any key to continue. ");
	    char c;
	    fflush(stdin);
	    scanf("%c", &c);
    }
	return 0;
}
Description

No description

To share this paste please copy this url and send to your friends
RAW Paste Data