[ create a new paste ] login | about

Link: http://codepad.org/FGYNzWRw    [ raw code | output | fork ]

C, pasted on May 5:
/* Bài 258/74/SBT Thầy NTTMK:Hãy sắp xếp các số nguyên tố trong mảng các số nguyên tăng dần,các giá trị khác giữ nguyên giá trị và vị trí. 161.cpp */
/* 

Tác giả: Nguyễn Việt Nam Sơn
Trung tâm đào tạo tin học - Thiết kế phần mềm - Sơn Đẹp Trai: www.SonDepTrai.com

Nguồn source code này Tôi viết vào năm 2012 lúc mới bắt đầu học lập trình C/C++ nên một số cách sẽ không được tối ưu - Bạn chỉ nên dùng trên tinh thần tham khảo thôi nhé.
Mong giúp đỡ được Bạn trên con đường Học Lập Trình.
TẤT CẢ VÌ SỰ THÀNH CÔNG CỦA BẠN

*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 10


void main()
{
int a[MAX], b[MAX];
int j = 0, temp;
bool bl;

// nhập mảng
for (int i = 0; i < MAX; i++)
{
printf("nhap 1 so nguyen: ");
scanf("%d", & a[i]);
}

// in ra mảng trước khi sắp xếp
printf("mang truoc sap xep: ");
for (int i = 0; i < MAX; i++)
printf(" %d", a[i]);

// Kiểm tra số nào là số nguyên tố, gán vị trí của nó vào mảng b
for (int i = 0; i < MAX; i++)
{
bl = true;	
for (int z = 2; z <= sqrt((float)a[i]); z++) 
{
if (a[i] % z == 0)
{
bl = false;
break;
}
}
if (bl)
{
b[j] = i;
j++;
}
}

// Sắp xếp các phần tử là số nguyên tố (vị trí đã được lưu trong mảng b) theo thứ tự giảm dần
for (int i = 0; i < j - 1; i++)
{
for (int k = i + 1; k < j; k++)
{
if(a[b[i]] > a[b[k]])
{
temp = a[b[i]];
a[b[i]] = a[b[k]];
a[b[k]] = temp;
}
} 
}

// in mảng sau khi sắp xếp
printf("\nmang sau khi sap xep: ");
for (int i = 0; i < MAX; i++)
printf(" %d", a[i]);

getch();
}
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAX 10


void main()
{
int a[MAX], b[MAX];
int j = 0, temp;
bool bl;

// nhập mảng
for (int i = 0; i < MAX; i++)
{
printf("nhap 1 so nguyen: ");
scanf("%d", & a[i]);
}

// in ra mảng trước khi sắp xếp
printf("mang truoc sap xep: ");
for (int i = 0; i < MAX; i++)
printf(" %d", a[i]);

// Kiểm tra số nào là số nguyên tố, gán vị trí của nó vào mảng b
for (int i = 0; i < MAX; i++)
{
bl = true;	
for (int z = 2; z <= sqrt((float)a[i]); z++) 
{
if (a[i] % z == 0)
{
bl = false;
break;
}
}
if (bl)
{
b[j] = i;
j++;
}
}

// Sắp xếp các phần tử là số nguyên tố (vị trí đã được lưu trong mảng b) theo thứ tự giảm dần
for (int i = 0; i < j - 1; i++)
{
for (int k = i + 1; k < j; k++)
{
if(a[b[i]] > a[b[k]])
{
temp = a[b[i]];
a[b[i]] = a[b[k]];
a[b[k]] = temp;
}
} 
}

// in mảng sau khi sắp xếp
printf("\nmang sau khi sap xep: ");
for (int i = 0; i < MAX; i++)
printf(" %d", a[i]);

getch();
}


Output:
Line 18: error: conio.h: No such file or directory
In function 'main':
Line 22: error: 'bool' undeclared (first use in this function)
Line 22: error: (Each undeclared identifier is reported only once
Line 22: error: for each function it appears in.)
Line 22: error: expected ';' before 'bl'
Line 25: error: 'for' loop initial declaration used outside C99 mode
Line 33: error: redefinition of 'i'
Line 25: error: previous definition of 'i' was here
Line 33: error: 'for' loop initial declaration used outside C99 mode
Line 37: error: redefinition of 'i'
Line 33: error: previous definition of 'i' was here
Line 37: error: 'for' loop initial declaration used outside C99 mode
Line 39: error: 'bl' undeclared (first use in this function)
Line 39: error: 'true' undeclared (first use in this function)
Line 40: error: 'for' loop initial declaration used outside C99 mode
Line 44: error: 'false' undeclared (first use in this function)
Line 56: error: redefinition of 'i'
Line 37: error: previous definition of 'i' was here
Line 56: error: 'for' loop initial declaration used outside C99 mode
Line 58: error: 'for' loop initial declaration used outside C99 mode
Line 71: error: redefinition of 'i'
Line 56: error: previous definition of 'i' was here
Line 71: error: 'for' loop initial declaration used outside C99 mode
Line 19: warning: return type of 'main' is not 'int'
t.c: At top level:
Line 83: error: redefinition of 'main'
Line 19: error: previous definition of 'main' was here
In function 'main':
Line 86: error: 'bool' undeclared (first use in this function)
Line 86: error: expected ';' before 'bl'
Line 89: error: 'for' loop initial declaration used outside C99 mode
Line 97: error: redefinition of 'i'
Line 89: error: previous definition of 'i' was here
Line 97: error: 'for' loop initial declaration used outside C99 mode
Line 101: error: redefinition of 'i'
Line 97: error: previous definition of 'i' was here
Line 101: error: 'for' loop initial declaration used outside C99 mode
Line 103: error: 'bl' undeclared (first use in this function)
Line 103: error: 'true' undeclared (first use in this function)
Line 104: error: 'for' loop initial declaration used outside C99 mode
Line 108: error: 'false' undeclared (first use in this function)
Line 120: error: redefinition of 'i'
Line 101: error: previous definition of 'i' was here
Line 120: error: 'for' loop initial declaration used outside C99 mode
Line 122: error: 'for' loop initial declaration used outside C99 mode
Line 135: error: redefinition of 'i'
Line 120: error: previous definition of 'i' was here
Line 135: error: 'for' loop initial declaration used outside C99 mode
Line 83: warning: return type of 'main' is not 'int'


Create a new paste based on this one


Comments: