HN - Aptech
Bạn có muốn phản ứng với tin nhắn này? Vui lòng đăng ký diễn đàn trong một vài cú nhấp chuột hoặc đăng nhập để tiếp tục.

HN - Aptech


 
Trang ChínhTrang Chính  PortalPortal  GalleryGallery  Latest imagesLatest images  Tìm kiếmTìm kiếm  Đăng kýĐăng ký  Đăng NhậpĐăng Nhập  

 

 [Code 2] Module test practical

Go down 
Tác giảThông điệp
spyware
Đại Bàng Tinh
Đại Bàng  Tinh
spyware


Tổng số bài gửi : 116
Join date : 04/06/2009
Age : 39
Đến từ : HN

[Code 2] Module test practical Empty
Bài gửiTiêu đề: [Code 2] Module test practical   [Code 2] Module test practical Icon_minitimeSat 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);
Về Đầu Trang Go down
spyware
Đại Bàng Tinh
Đại Bàng  Tinh
spyware


Tổng số bài gửi : 116
Join date : 04/06/2009
Age : 39
Đến từ : HN

[Code 2] Module test practical Empty
Bài gửiTiêu đề: Re: [Code 2] Module test practical   [Code 2] Module test practical Icon_minitimeSat 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.
Về Đầu Trang Go down
spyware
Đại Bàng Tinh
Đại Bàng  Tinh
spyware


Tổng số bài gửi : 116
Join date : 04/06/2009
Age : 39
Đến từ : HN

[Code 2] Module test practical Empty
Bài gửiTiêu đề: Re: [Code 2] Module test practical   [Code 2] Module test practical Icon_minitimeSat 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]);
}
Về Đầu Trang Go down
Sponsored content





[Code 2] Module test practical Empty
Bài gửiTiêu đề: Re: [Code 2] Module test practical   [Code 2] Module test practical Icon_minitime

Về Đầu Trang Go down
 
[Code 2] Module test practical
Về Đầu Trang 
Trang 1 trong tổng số 1 trang
 Similar topics
-
» [Code 1] :Module test practical
» code 09 thiếu EXAM1 mời các bác vào chém code hay cực (menu)
» code 19 đã ok các chú các bác vào chém đê !!!!!!!!!!!(code fia dứơi nhé)
» code:08/day du!/code nay kha hay, cac ban tham khao!!
» code:14/question2 va question3

Permissions in this forum:Bạn không có quyền trả lời bài viết
HN - Aptech :: Khóa Học :: SEMESTER I :: C :: Bài Tập-
Chuyển đến