PAT BASIC Programming Topic set--function set (1~5) __pat

Source: Internet
Author: User
4-1 Simple output integer (10 points)

This requires implementing a function that prints all positive integers from 1 to n for a given positive integer n. function Interface Definition:

void printn (int N);

where n is the parameter passed in by the user. The function must print out all the positive integers from 1 to n, each of which takes 1 rows. Sample Referee Test procedure:

#include <stdio.h>

void printn (int N);

int main ()
{
    int N;

    scanf ("%d", &n);
    PRINTN (N);

    return 0;
}

/* Your code will be embedded here * *
Input Sample:
3
Output Sample:
1
2
3

Solution Program:

void printn (int N)
{
int i;
for (i=1;i<=n;i++)
printf ("%d\n", I);
}


——————————————————————————————————————————————————————————————————

4-2 polynomial evaluation (15 points)

This requires the implementation of a function, the calculation of the order of N, the coefficient of a[0] ... a[n] polynomial f (x) =∑i=0n (A[I]XXI) f (x) =\sum_{i=0}^{n} (A[i]\times x^i) f (x) =∑i=0 n (a[i]xx i) in X The value of the point. function Interface Definition:

Double f (int n, double a[], double x);

where n is the order of polynomials, a[], and X is the given point. The function must return the value of the polynomial f (x). Sample Referee Test procedure:

#include <stdio.h>

#define MAXN

double F (int n, double a[], double x);

int main ()
{
    int n, i;
    Double A[MAXN], X;
				
    scanf ("%d%lf", &n, &x);
    For (i=0 i<=n; i++)
        scanf ("%lf", &a[i));
    printf ("%.1f\n", f (N, a, x));
    return 0;
}

/* Your code will be embedded here * *
Input Sample:
2 1.1
1 2.5-38.7
Output Sample:
-43.1

Solution Program:

Double f (int n, double a[], double x)
{
int i;
Double mutlipy=1.0,sum=0;
for (i=0;i<=n;i++) {
sum+=a[i]*mutlipy;
Mutlipy*=x;
}
return sum;
}


——————————————————————————————————————————————————————————————————



4-3 Simple sum (10 points)

This requires the implementation of a function to find the number of n integers given. function Interface Definition:

int Sum (int list[], int N);

Where the given integer is stored in the array list[], the positive integer n is the number of elements in the array. The function must return the and of N list[] elements. Sample Referee Test procedure:

#include <stdio.h>

#define MAXN

int Sum (int list[], int N);

int main ()
{
    int list[maxn], N, I;

    scanf ("%d", &n);
    For (i=0 i<n; i++)
        scanf ("%d", &list[i]);
    printf ("%d\n", Sum (List, N));

    return 0;
}

/* Your code will be embedded here * *
Input Sample:
3
12 34-5
Output Sample:
41


Solution Program:

int Sum (int list[], int N)
{
int i,sunshine=0;
for (i=0;i<n;i++)
{
Sunshine+=list[i];
}

return sunshine;
}


——————————————————————————————————————————————————————————————————


4-4 the average (10 points) of a custom type element

This requires the implementation of a function to find the average of n-set elements s[], where the type of the collection element is a custom ElementType. function Interface Definition:

ElementType Average (ElementType s[], int N);

Where the given set element is stored in the array s[], the positive integer n is the number of elements in the array. The function must return an average of n s[] elements, and the value should also be of the ElementType type. Sample Referee Test procedure:

#include <stdio.h>

#define MAXN a
typedef float ElementType;

ElementType Average (ElementType s[], int N);

int main ()
{
    ElementType s[maxn];
    int N, I;

    scanf ("%d", &n);
    For (i=0 i<n; i++)
        scanf ("%f", &s[i));
    printf ("%.2f\n", Average (S, N));

    return 0;
}

/* Your code will be embedded here * *
Input Sample:
3
12.3 34-5
Output Sample:
13.77


Solution Program:

ElementType Average (ElementType s[], int N)
{
ElementType average=0;
int i;
for (i=0;i<n;i++) {
Average+=s[i];
}
Average/=n;
return average;
}

——————————————————————————————————————————————————————————————————


4-5 the maximum value of a custom type element (10 points)

The main requirement is to implement a function to find the maximum value in the N-set element s[], where the type of the collection element is a custom ElementType. function Interface Definition:

ElementType Max (ElementType s[], int N);

Where the given set element is stored in the array s[], the positive integer n is the number of elements in the array. The function must return the maximum value in n s[] elements, and the value should also be of type ElementType. Sample Referee Test procedure:

#include <stdio.h>

#define MAXN a
typedef float ElementType;

ElementType Max (ElementType s[], int N);

int main ()
{
    ElementType s[maxn];
    int N, I;

    scanf ("%d", &n);
    For (i=0 i<n; i++)
        scanf ("%f", &s[i));
    printf ("%.2f\n", Max (S, N));

    return 0;
}

/* Your code will be embedded here * *
Input Sample:
3
12.3 34-5
Output Sample:
34.00


Solution Program:

ElementType Max (ElementType s[], int N)
{
    ElementType    max=s[0]; & nbsp;   //Note that Max cannot be used to randomly assign a value of 0, and if you enter a negative number, you will receive an incorrect return value;
    int i
    if (N>MAXN)     {
        n=maxn;
    
    for (i=0;i<n;i++)     {
        if (s[i) >max)     {
            max=s[i];
        }    
    
    return max;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.