Chapter 9 Experimental Guidelines

Chapter 9 Experimental Guidelines

 

  • Question 9.3:

Using the function Swap() in the example 9.6 program, input 10 integers from the keyboard, use function programming to calculate the maximum and minimum values, and exchange their positions in the array.

 

1. Problem solving ideas:

   In the main function, define the array, use the pointer to define the maximum value, the minimum value and its position, and assign the initial value to the pointer, enter 10 numbers in the loop, declare the output function in the outer function prototype, call this function, and output the initial array number . The outer function prototype declaration, use the pointer to point to the maximum value, the minimum value and its position, the main function call, the outer layer defines the maximum value, the minimum position starts at 0, and the comparison is performed in the loop if(a[j]>=a [m]) m=j; if(a[j]<=a[p]) p=j; Use the pointer to point out the maximum value, minimum value and its position. Output maximum and minimum values. To exchange the positions of the maximum and minimum values, the outer function declares the exchange function, defines the temp, after the exchange, it is brought out with a pointer, and the exchanged array is output.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

int n=10;

void Swap(int *a,int *b);

void printfnum(int a[],int n);

void Findnum(int a[],int n,int *pMax,int *pMin,int *pMaxpos,int *pMinpos);

int main ()

{

    int i,c,d;

    int a[n],Max,Min,Maxpos,Minpos,*pMax,*pMin,*pMaxpos,*pMinpos;

    pMax=&Max;

    pMin=&Min;

    pMaxpos=&Maxpos;

    pMinpos=&Minpos;

    for(i=0;i<n;i++)

    {

        scanf("%d",&a[i]);

    }

    printfnum(a,n);

    Findnum(a,n,pMax,pMin,pMaxpos,pMinpos);

    printf("Max = %d",*pMax);

    printf("Min = %d",*pMin);

    printf("\n");

    c=&a[*pMaxpos];

    d=&a[*pMinpos];

    Swap(c,d);

    printfnum(a,n);

    return 0;

}

void Swap(int *a,int *b)

{

    int temp;

    temp=*a;

    *a=*b;

    *b=temp;

}

void printfnum(int a[],int n)

{

    int i;

    for(i=0;i<n;i++)

    {

        printf("%5d",a[i]);

    }

    printf("\n");

}

void Findnum(int a[],int n,int *pMax,int *pMin,int *pMaxpos,int *pMinpos)

{

    int i,j,m=0,p=0;

    for(i=0;i<n;i++)

    {

        for(j=0;j<n;j++)

        {

            if(a[j]>=a[m]) m=j;

            if(a[j]<=a[p]) p=j;

        }

    }

    *pMax=a[m];

    *pMin=a[p];

    * pMaxpos = m;

    *pMinpos=p;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  • Topic 2:

Use letter programming to solve the following date conversion problem according to the following function prototype (requires considering the leap year problem)

(1) Enter a certain year, a certain month, and a certain day, calculate and output that it is the day of the year:

/*Function function: For a given year, month, and day, calculate the day of the year.

    Function parameters: integer variables year, month, day, respectively representing year, month and day

Function return value: the day of the year*/

 

  1. Problem-solving ideas:

Define the leap year and the year respectively, enter the year, month and day in the main function, determine whether it is a leap year, call the outer function, accumulate in the leap year, a one-dimensional array, and loop, add the input number, and bring out the pointer. In normal years, similar methods are called out.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

void DayofYear(int Mouth,int Day,int *Days);

void DayofYear1(int Mouth,int Day,int *Days);

int main ()

{

    int YearDay,*Days=&YearDay;

    int Year,Mouth,Day;

    printf("Year:");

    scanf("%d",&Year);

    printf("Mouth:");

    scanf("%d",&Mouth);

    printf("Day:");

    scanf("%d",&Day);

    if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))

    {

         printf("Year is leap year");

         DayofYear(Mouth,Day,Days);

         printf("%d day",*Days);

    }

      else

      {

         printf("Year is not leap year");

         DayofYear1(Mouth,Day,Days);

         printf("%d day",*Days);

      }

    return 0;

}

void DayofYear(int Mouth,int Day,int *Days)

{

    int sum=0,i;

    int a[]={31,29,31,30,31,30,31,31,30,31,30,31};

    for(i=0;i<Mouth-1;i++)

    {

        sum=sum+a[i];

    }

    sum=Day+sum;

    *Days=sum;

}

void DayofYear1(int Mouth,int Day,int *Days)

{

    int sum=0,j;

    int b[]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(j=0;j<Mouth-1;j++)

    {

        sum=sum+b[j];

    }

    sum=Day+sum;

    *Days=sum;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

  • Question 3:

2) Input the day of a certain year, calculate and output the month and day of the year.

Function: For a given year, calculate the month and day of the year

Function population parameter: integer variable year, storage year

Integer variable yearDay, stores the day of the year

Function exit parameters: integer pointer pMonth, which points to an integer variable that stores the month of the year

Integer pointer pDay, points to an integer variable that stores the day

Function return value: None*/

 

  1. Problem-solving ideas:

   Enter the year and number of days in the main function to determine leap years and average years. Call outer function, output month and day. The outer function For loop, judgment sentence, until yearday is less than the value in the array, jump out of the loop. Use the pointer to bring out the month and day.

 

2. Source code:

#include <stdio.h>

#include <stdlib.h>

void DayofYear(int *MouthDay,int *pMouth,int YearDay);

void DayofYear1(int *MouthDay,int *pMouth,int YearDay);

int main ()

{

    int Mouth,*pMouth=&Mouth;

    int Year,YearDay;

    int Days,*MouthDay=&Days;

    printf("Year:");

    scanf("%d",&Year);

    printf("YearDay:");

    scanf("%d",&YearDay);

    if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))

    {

         printf("Year is leap year");

         DayofYear(MouthDay,pMouth,YearDay);

         printf("%d day of month %d",*pMouth,*MouthDay);

    }

      else

      {

         printf("Year is not leap year");

         DayofYear1(MouthDay,pMouth,YearDay);

         printf("%d day of month %d",*pMouth,*MouthDay);

      }

    return 0;

}

void DayofYear(int *MouthDay,int *pMouth,int YearDay)

{

    int i;

    int a[]={31,29,31,30,31,30,31,31,30,31,30,31};

    for(i=0;YearDay>a[i];i++)

    {

        YearDay=YearDay-a[i];

    }

 

    * pMouth = i;

    *MouthDay=YearDay;

}

void DayofYear1(int *MouthDay,int *pMouth,int YearDay)

{

    int j;

    int b[]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(j=0;YearDay>b[j];j++)

    {

        YearDay=YearDay-b[j];

    }

 

    * pMouth = j;

    *MouthDay=YearDay;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

  • Question 4:

(3) Enter the following menu and use the switch statement to perform corresponding operations according to the user's input:

1.year/ month/ day  ->  yearDay

2.yearDay -> year/ month/ day

3. Exit

Please enter your choice:

 

1. Problem solving ideas:

Switch chooses the desired option.

  1. Source code:

#include <stdio.h>

#include <stdlib.h>

void DayofYear(int Mouth,int Day,int *pDays);

void DayofYear1(int Mouth,int Day,int *pDays);

void DayofYear2(int *MouthDay,int *pMouth,int YearDay);

void DayofYear3(int *MouthDay,int *pMouth,int YearDay);

 

int main ()

{

    int num;

    int YearDay,*pDays=&YearDay;

    int Year,Mouth,Day;

    int kMouth,*pMouth=&kMouth;

    int Days,*MouthDay=&Days;

    printf("Enter 1 to Yearday:\n");

    printf("Enter 2 to Mouth and:\n");

    printf("Exist enter your choice:\n");

    scanf("%d",&num);

    switch(num)

    {

        for(;num !=1 && num != 2;)

        {

           case 3:

              printf("Please enter your choice:");

              break;

           case 1:

             printf("Year:");

             scanf("%d",&Year);

             printf("Mouth:");

             scanf("%d",&Mouth);

             printf("Day:");

             scanf("%d",&Day);

             if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))

              {

               printf("Year is leap year");

               DayofYear(Mouth,Day,pDays);

               printf("%d day",*pDays);

               }

            else

              {

               printf("Year is not leap year");

               DayofYear1(Mouth,Day,pDays);

               printf("%d day",*pDays);

              }

              break;

           case 2:

               printf("Year:");

               scanf("%d",&Year);

               printf("YearDay:");

               scanf("%d",&YearDay);

               if((Year % 4 == 0)&&(Year % 100 != 0)||(Year % 400 == 0))

                {

                printf("Year is leap year");

                DayofYear2(MouthDay,pMouth,YearDay);

                printf("%d month and %d day",*pMouth,*MouthDay);

                }

                else

                {

                printf("Year is not leap year");

                DayofYear3(MouthDay,pMouth,YearDay);

                printf("%d month and %d day",*pMouth,*MouthDay);

                }

             break;

            }

        }

 

 

    return 0;

}

void DayofYear(int Mouth,int Day,int *pDays)

{

    int sum=0,i;

    int a[]={31,29,31,30,31,30,31,31,30,31,30,31};

    for(i=0;i<Mouth-1;i++)

    {

        sum=sum+a[i];

    }

    sum=Day+sum;

    *pDays=sum;

}

void DayofYear1(int Mouth,int Day,int *pDays)

{

    int sum=0,j;

    int b[]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(j=0;j<Mouth-1;j++)

    {

        sum=sum+b[j];

    }

    sum=Day+sum;

    *pDays=sum;

}

void DayofYear2(int *MouthDay,int *pMouth,int YearDay)

{

    int i;

    int a[]={31,29,31,30,31,30,31,31,30,31,30,31};

    for(i=0;YearDay>a[i];i++)

    {

        YearDay=YearDay-a[i];

    }

 

    * pMouth = i;

    *MouthDay=YearDay;

}

void DayofYear3(int *MouthDay,int *pMouth,int YearDay)

{

    int j;

    int b[]={31,28,31,30,31,30,31,31,30,31,30,31};

    for(j=0;YearDay>b[j];j++)

    {

        YearDay=YearDay-b[j];

    }

 

    * pMouth = j;

    *MouthDay=YearDay;

}

 

3. Screenshot of program running effect:

 

 

 

 

 

 

 

 

 

 

  • MOOC web course test results:

1. Test 1:

Guess you like

Origin blog.csdn.net/weixin_48450741/article/details/112464879