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 2] Module test practical Sat Jun 13, 2009 8:16 pm | |
| //Code 2 /* Question 1 Declare function do the followings: - Code:
-
//- Input array A have two dimension N and M and array store real number. void input(float A[][100],int n,int m); //- Calculate total value of N real number. float sum(float a[],int n); //- Check whether an integer is even or odd int check(int n); // return 1 if true else return 0 //- Swap value of two integer numbers. void swap(int *a, int *b); | |
|
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 2] Module test practical Sat Jun 13, 2009 8:17 pm | |
| //Code 2 /* Question 2 Write a program to accep N strings (N<=50), store them in separate of an array, and then finds the average string (finds average length), show all the strings have length greater than average string and the total of them. The program must be divided into 4 mudules which do the followings: - Accept N and elements of the array from user's input (0 - Calculator average length of strings - Display all strings that have length greater than average length - Write function main() to call all function.*/ - Code:
-
#include<conio.h> #include<stdio.h> #include<string.h> #include<stdlib.h> // declare function void input(int *n, char (*P)[100]); // 0<n<=50 float aver(int n, char (*p)[100]); void display(int n, char (*p)[100]); // define main function void main() { int i,n; float avers; // char (*p)[100]; char p[50][100]; clrscr(); input(&n,p); avers=aver(n,p); printf("\nAver length of strings is : %8.2f",avers); display(n,p); free(p); getch(); } // define input function void input(int *n, char (*p)[100]) // 0<n<=50 { int i; do { printf("\nNhap N = "); scanf("%d",n); } while(*n<=0 || *n>50); // p=(char (*)[100])malloc(*n*sizeof(char[100])); for(i=0;i<*n;i++) { printf("\nString %d : ",i+1); fflush(stdin); gets(p[i]); } } //define aver function float aver(int n, char (*p)[100]) { int s=0,i; for(i=0;i<n;i++) s+=strlen(p[i]); return (s/n); } //define display function void display(int n, char (*p)[100]) { int i,count=0; printf("\nGreater string(s): \n"); for(i=0;i<n;i++) { if(aver(n,p)<strlen(p[i])) { puts(p[i]); count++; } } printf("\nTotal: %d",count); }
Được sửa bởi spyware ngày Mon Jun 15, 2009 10:27 am; sửa lần 1. | |
|
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 2] Module test practical Sat Jun 13, 2009 8:18 pm | |
| /*Question 3 Write a menu-based program with the following menu and call in function main: 1. Input N and array of integer 2. Find the minimum positive number value 3. Count the number that is not prime numbers 4. Sort all element by descending and display them 5. Exit Q1. Input N and array of integer when user chooses 1 from main menu, do the following: - First, display "N= " and accept positive integer N (0 - then, accept N integer and store them into array Q2. Find the minimum positive number value When user chooses 2 from main menu, find the minimum positive number value in the array( which array in the first menu) and return value to the main function for printing out. note: if the array has no positive number, return 0. Q3. count the number that is not prime numbers when user chooses 3 from main menu, count the number that is not prime number and return it to the main function for printing out (if the array has all prime number, display 0) Q4. sort all element by descending and display them when use chooses 5 from main menu, sort all element by descending and display Q5. when user chooses 5 from main menu, the program will be exit */ - Code:
-
#include<conio.h> #include<stdio.h> #include<stdlib.h> // define all function void menu (void); void input(int *n,int a[]);// 0<n<100 void show(int n, int a[]); int minp(int n,int a[]); int checkprime(int n);// return 1 if true else return 0 int countp(int n,int a[]); void sort(int n,int a[]);
// define main function void main() { int choice,p[100],n; clrscr(); do { clrscr(); menu(); scanf("%d",&choice); switch(choice) { case 1: input(&n,p);getch(); break; case 2: show(n,p); printf("The minimum in array is %d",minp(n,p)); getch(); break; case 3: show(n,p); printf(" Has %d numbers that isn't prime number",countp(n,p)); getch(); break; case 4: show(n,p); sort(n,p); getch(); break; case 5: exit(0); } } while(choice!=5); getch(); }
// define menu function void menu(void) { printf("\n1. Input N and array of integer"); printf("\n2. Find the minimum positive number value"); printf("\n3. Count the number that is not prime numbers"); printf("\n4. Sort all element by descending and display them"); printf("\n5. Exit"); printf("\n Enter your choice! "); }
//define input function ( 0<n<1000 void input(int *n,int a[]) { int i; do { printf("N= "); scanf("%d",n); } while(*n<=0||*n>=100); for(i=0;i<*n;i++) { printf("a[%d]= ",i); scanf("%d",&a[i]); } }
// define show function void show(int n,int a[]) { int i; printf("\nArray inputted: ( there are %d elements)\n",n); for(i=0;i<n;i++) { printf("%4d",a[i]); } printf("\n"); }
// define minp function int minp(int n, int a[]) { int i,min,kt,j; for(i=0;i<n;i++) if(a[i]>0) { min=a[i]; kt=1; break; } if(kt!=1) return 0; for(j=i+1;j<n;j++) if(a[j]>0 && a[j]<min) min=a[j]; return min; }
//define checkprime function int checkprime(int n) { int i; if(n<=1) return 0; for(i=2;i<n;i++) if(n%i==0) return 0; return 1; }
// define countp function int countp(int n, int a[]) { int i,count=0; for(i=0;i<n;i++) if(checkprime(a[i])==0) count++; if(count!=0) return count; return 0; } //define sort function by descending void sort(int n,int a[]) { int i,j,temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) { if(a[i]<a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } printf("\nArray after sorted:"); for(i=0;i<n;i++) printf("%4d",a[i]); }
| |
|
Sponsored content
| Tiêu đề: Re: [Code 2] Module test practical | |
| |
|