Code bài tập về mảng không xuất được mảng

Mình không xuất được mảng

void input(float a[100], int n){
     int i;
do{
            printf("In put size of array: ");
		scanf("%d",&n);
		printf("Enter %d float number: ",n);
		for(i=0;i<=n-1;i++){
			scanf(" %f",&a[i]);
		}
	}while(n<0||n>100);
}
 void display(float a[100], int n){
 	int i; 
 	for (i=0; i<=n-1; i++)
 		printf("%5.1f\t",i, a[i]);
}
  void bubbleSort(float a[100], int n)
{ int i, j;
  int temp;
  printf("before sorting: ");
  display (a,n);
    for (i=n-1;i>0;i--)
    { if (a[j]>a[j+1])
    {temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
	}
}
    printf("\nafter sorting: ");
    display(a,n);
}
void search (float a[100], int n, float x){
	int i;
	printf("Enter the value x to be searched: ");
	scanf("%.1f", &x);
	for(i=0; i<n; i++){
		if (x==a[i]){
			printf("Found %f at point %d", x,i);
		}
	else {
		printf ("Error!");}
}
}
 int main (){
 	int n, check;
 	float x, a[100];
 	do{
	 printf ("\nSelect opions:\n");
 	printf("1. Enter the list of float numbers\n");
 	printf("2. Sort the list in increasing order\n");
 	printf("3. Search value x in the list\n");
 	printf("4. Display the list\n");
 	printf("0.Exit\n");
 	printf("Your selection (0 -> 4): ");
 	scanf("%d", &check);
 	switch(check){
 	case 1: input(a,n);	break;
    case 2: bubbleSort(a,n); break;
    case 3: search(a,n,x); break;
    case 4: display(a,n); break;
    case 0: return 0;
      }
    } while (check!=0);
}
Beautify
void input(float a[100], int n) {
    int i;
    do {
        printf("In put size of array: ");
        scanf("%d", & n);
        printf("Enter %d float number: ", n);
        for (i = 0; i <= n - 1; i++) {
            scanf(" %f", & a[i]);
        }
    } while (n < 0 || n > 100);
}
void display(float a[100], int n) {
    int i;
    for (i = 0; i <= n - 1; i++)
        printf("%5.1f\t", i, a[i]);
}
void bubbleSort(float a[100], int n) {
    int i, j;
    int temp;
    printf("before sorting: ");
    display(a, n);
    for (i = n - 1; i > 0; i--) {
        if (a[j] > a[j + 1]) {
            temp = a[j];
            a[j] = a[j + 1];
            a[j + 1] = temp;
        }
    }
    printf("\nafter sorting: ");
    display(a, n);
}
void search(float a[100], int n, float x) {
    int i;
    printf("Enter the value x to be searched: ");
    scanf("%.1f", & x);
    for (i = 0; i < n; i++) {
        if (x == a[i]) {
            printf("Found %f at point %d", x, i);
        } else {
            printf("Error!");
        }
    }
}
int main() {
    int n, check;
    float x, a[100];
    do {
        printf("\nSelect opions:\n");
        printf("1. Enter the list of float numbers\n");
        printf("2. Sort the list in increasing order\n");
        printf("3. Search value x in the list\n");
        printf("4. Display the list\n");
        printf("0.Exit\n");
        printf("Your selection (0 -> 4): ");
        scanf("%d", & check);
        switch (check) {
        case 1:
            input(a, n);
            break;
        case 2:
            bubbleSort(a, n);
            break;
        case 3:
            search(a, n, x);
            break;
        case 4:
            display(a, n);
            break;
        case 0:
            return 0;
        }
    } while (check != 0);
}
1 Like

Bạn truyền 2 đối số (i, a[i]), nhưng chuỗi định dạng chỉ có 1 (%5.1f).

2 Likes

Không liên quan, nhưng bạn thật sự cần format lại cái code đó.

1 Like

bạn có thể nói rõ hơn ko ạ, mình không hiểu ý bạn lắm

1 Like

mình xóa i đi r vẫn ko được thì phải làm sao ạ

Không được!
Cụ thể nó bị gì? :man_facepalming::woman_facepalming:

1 Like

đầu bài nó yêu cầu kiểu này.


lúc mình chạy thì nó thế này

Ý của mình là bạn nên format (tức định dạng lại cái code sao cho nó đẹp và dễ đọc hơn), vì mình thề là cái code bạn đăng không phải do người bình thường code.

2 Likes

à ok, mik sẽ sửa để nó đẹp hơn, cảm ơn bạn

Tham trị và con trỏ.

Vì biến n không được gán đúng giá trị sau khi thoát khỏi hàm nhập. Bạn đang dùng tham trị, giá trị biến truyền vào không được thay đổi. Bạn nên dùng con trỏ hoặc trả về giá trị của n trong hàm nhập.
Bạn có thấy scanf() phải dùng & với biến n không?

1 Like

B thử dùng con trỏ cho n đi… thử cho nhập n vào hàm main xem

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?