spyware Đại Bàng Tinh
Tổng số bài gửi : 116 Join date : 04/06/2009 Age : 39 Đến từ : HN
| Tiêu đề: [Code 1] :Module test practical Fri Jun 12, 2009 11:36 pm | |
| - Code:
-
/*Question 3: Write menu-based program with the following menu: 1. Input N and array of integer 2. Find the largest negative number value 3. Count the number that is not square numbers 4. sort all elements by descending and display them 5. Exit
Q1: Input N and array of integer when user chooses 1 from main menu, do the followings - fist, display 'N=' and accept positive integer N - then, accept N integer and store them into array Q2: Find the largest negative number value when user chooses 2 from main menu, find the largest numbet value in the array (which accept in the first menu) and return this value to the main function for printing out. (Note: if the array has no negative number, return 0) Q3: Count the number that is no square numbers when the user chooses 3 from main menu, count the number that is not square number and return it to the main function for printing out (it the array has no numbe, dispay 0) Q4: Sort all element by decending and display them when user chooses 4 from main menu, sort all elements by decending and display Q5: Exit when user chooses 5 from main menu, the program will be exit. */
#include<conio.h> #include<stdio.h> // khai bao ham con void input(int a[], int *n); void show(int a[], int n); // hien thi mang vua nhap int maxne(int a[], int n);// tra ve cho ham main in ra int checkcp(int n); // kiem tra so chinh phuong int notcp(int a[], int n); // tra ve cho ham main in ra void sort(int a[],int n);//sau khi sap xep hien thi ngay trong ham con void menu(void); // Define main function void main() { int choice,i,n,p[100]; clrscr(); do { clrscr(); menu(); scanf("%d",&choice); switch(choice) { case 1: input(p,&n); getch(); break; case 2: show(p,n); printf("\nThe largest negative number is: %d",maxne(p,n)); getch(); break; case 3: show(p,n); printf("\nHas [%d] number that is not square number.",notcp(p,n)); getch(); break; case 4: show(p,n);sort(p,n); getch(); break; case 5: exit(0); } } while(choice!=5); getch(); } //// Define functions //// //define menu function void menu(void) { printf("\n1. Input N and array of integer"); printf("\n2. Find the largest negative number value"); printf("\n3. Count the number that is not square numbers"); printf("\n4. Sort all elements by decending and display them"); printf("\n5. Exit"); printf("\n Pleases enter your choice !"); } //Define input function void input(int a[],int *n) { int i; printf("N= "); scanf("%d",n); for(i=0;i<*n;i++) { printf("a[%d]= ",i); scanf("%d",&a[i]); }
} // Define show function void show(int a[],int n) { int i; printf("\nInput array:"); for(i=0;i<n;i++) printf("%5d",a[i]); } // Define function to find the largest negative number int maxne(int a[],int n) { int i,j,am,kt=0; for(i=0;i<n;i++) { if(a[i]<0) { am=a[i]; kt++; break; } } for(j=i+1;j<n;j++) { if(a[j]<0 && a[j]>am) am=a[j]; } if(kt) return am; return 0; } // define check square number function int checkcp(int n) { int i,kt=0; // if(n<0) return 0; for(i=1;i<=n;i++) if(i*i==n) kt++; if(kt) return 1; return 0; } // define count number that not square number function int notcp(int a[],int n) { int dem=0,i,j; for(i=0;i<n;i++) { if(checkcp(a[i])==0) dem++; } if(dem==0) return 0; return dem;
} // define sort function void sort(int a[],int n) { int i,j,temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(a[j]>a[i]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("\n"); for(i=0;i<n;i++) printf("%5d",a[i]); }
| |
|
spyware Đại Bàng Tinh
Tổng số bài gửi : 116 Join date : 04/06/2009 Age : 39 Đến từ : HN
| Tiêu đề: Re: [Code 1] :Module test practical Fri Jun 12, 2009 11:49 pm | |
| /* Question 1 */
/*1. Find and return max and min of N real number */ void minmax(float a[], int n,float *min,float *max);
/*2. Find and return the largest string of N strings */ void maxstr(char (*p)[100], int n,char *maxs);
/*3. Check whether an integer is prime or not */ int ktrahh(int n);
/*4. Funtion for printing N float numbers */ void inf(float a[],int n); | |
|
spyware Đại Bàng Tinh
Tổng số bài gửi : 116 Join date : 04/06/2009 Age : 39 Đến từ : HN
| Tiêu đề: Re: [Code 1] :Module test practical Sat Jun 13, 2009 7:09 pm | |
| - Code:
-
/* Question 2 Write a program to accept one string, after that count the number of words and trim all space character at the last of string (you must print length of string before and after trim). the program must be divided into three modules which do the followings: - Accept one string from user's input - Count the number of words - Trim all space character at the last of string (you must be printf length of string before and after trim).*/
#include<conio.h> #include<stdio.h> #include<string.h> void input(char a[]); int count(char a[]); void cuts(char a[]); // Define main function void main() { char str[100]; clrscr(); input(str); printf("Numbers of words is: %d",count(str)); cuts(str); getch(); } // Define function // Define input function void input(char a[]) { printf("Input string:"); fflush(stdin); gets(a); } // Define count function int count(char a[]) { int i,j,n,dem; n=strlen(a); for(i=0,dem=0; i<n; i++) if(a[i]!=' '&& a[i]!='\t') { for(j=i+1;j<n;j++) { if(a[j]!=' '&& a[j]!='\t'&& a[j]!='\0') continue; else { dem++; i=j; break; } } } return dem; } // Define Cuts function void cuts(char a[]) { int i,n; n=strlen(a); printf("\nLength of string before trim: %d",n); for(i=n-1;i>=0;i--) if(a[i]==' '|| a[i]=='\t') a[i]='\0'; else break; printf("\nString after trim: "); puts(a); printf("Length of string after trim: %d",strlen(a)); }
| |
|
spyware Đại Bàng Tinh
Tổng số bài gửi : 116 Join date : 04/06/2009 Age : 39 Đến từ : HN
| Tiêu đề: Re: [Code 1] :Module test practical Sat Jun 13, 2009 7:39 pm | |
| | |
|
Join date : 01/01/1970
| Tiêu đề: Re: [Code 1] :Module test practical Mon Jun 29, 2009 1:29 am | |
| sao em làm code ham trim nó ko cắt khoảng trắng nhờ????????? | |
|
Sponsored content
| Tiêu đề: Re: [Code 1] :Module test practical | |
| |
|