Abstract thinking of C Programming 2.10 programming exercises (unfinished)

Source: Internet
Author: User

Address: Workshop.

2. Calculate the surface area and volume of the cylinder as required.

# Include <stdio. h> # include <math. h> # include <stdbool. h> # define PI 3.1415926 void input (double * r, double * h) {double a, B; printf ("Enter the radius and Height :"); scanf ("% lf", & a, & B); * r = a; * h = B;} void count (double r, double h, double * s, double * v) {* s = (2 * PI * r) * h; * v = PI * r * h;} void output (double s, double v) {printf ("cylindrical surface area: % lf \ n", s); printf ("cylindrical volume: % lf \ n", v);} int main () {double r, h, s, v; input (& r, & h); count (r, h, & s, & v); output (s, v ); return 0 ;}

3. Calculate the average value according to the rule.

# Include <stdio. h> # include <math. h> # include <stdbool. h> # define MaxJudges 100 double Mean (double array [], int n) {int I; double total, max, min; max = min = array [0]; total = 0.0; for (I = 0; I <n; I ++) {total + = array [I]; if (max <array [I]) max = array [I]; if (min> array [I]) min = array [I];} return (total-min-max)/n );} void ReadScore (double scores [], int n) {int I; for (I = 0; I <n; I ++) scanf ("% lf ", & scores [I]);} int main () {int n; double array [MaxJudges]; scanf ("% d", & n); ReadScore (array, n ); printf ("average value: % lf \ n", Mean (array, n); return 0 ;}

4. Determine whether a series is in ascending order.

#include<stdio.h>#include<math.h>#include<stdbool.h>bool IsSorted(int *a, int n){    int i;    for(i = 1; i <= n - 1; i++) {        if(a[i] < a[i-1])            return false;    }    return true;}int main(){    int n, array[1000];    scanf("%d", &n);    for(int i = 0; i < n; i++)        scanf("%d", &array[i]);    if(IsSorted(array, n))        printf("TRUE\n");    else        printf("FALSE\n");    return 0;}   

5. Generate 2 ~ of the etatosney sieve ~ Prime number between 1000

#include<stdio.h>#include<stdbool.h>#include<math.h>#define N 1000000bool isprime[N];void initPrime(){    int n, i, j;    int k;    for(k = 0; k < N; k++)         isprime[k] = true;    n = (int)sqrt(N);    for(i = 2; i <= n; i++) {        if(isprime[i]) {            j = i * i;            while(j <= n) {                isprime[j] = false;                j += i;            }        }    }}int main(){    int i = 2;    initPrime();    while(1) {        if(i > 1000) break;        if(isprime[i])            printf("%d\n", i);        i++;    }    return 0;}

6. output the column chart of a group of data according to the format

#include<stdio.h>#include<stdbool.h>#include<math.h>void printStar(int n){    for(int i = 1; i <= n; i++)        putchar('*');    printf("\n");}int main(){    int a[11], b[1000];    int n, t, i;    scanf("%d", &n);    for(i = 0; i < 11; i++)        a[i] = 0;    for(i =0; i < n; i++) {        scanf("%d", &b[i]);        t = b[i] / 10;        switch(t) {            case 0: a[0]++; break;            case 1: a[1]++; break;            case 2: a[2]++; break;            case 3: a[3]++; break;            case 4: a[4]++; break;            case 5: a[5]++; break;            case 6: a[6]++; break;            case 7: a[7]++; break;            case 8: a[8]++; break;            case 9: a[9]++; break;            case 10: a[10]++; break;            default: break;        }    }    for(i = 0; i < 11; i++) {        printf("%3d: ", i * 10);        printStar(a[i]);    }    return 0;}

8. Scan the array. If the array contains 0 elements, the number of 0 elements is returned.

#include<stdio.h>#include<stdbool.h>#include<math.h>void swap(int *a, int *b){    int t;    t = *a;    *a = *b;    *b = t;}int RemoveZeroElements(int a[], int n){    int num, i, j;    num = 0;    if(a[0]) num++;    for(i = 0; i < n; i++) {        if(!a[i] && i < n - 1) {            for(j = i + 1; j < n; j++) {                if(a[j]) {                    num++;                    swap(&a[i], &a[j]);                    break;                }            }        }    }    return n - num;}void printArray(int *a, int n){    int i;    for(i = 0; i < n; i++)        printf("%d ", a[i]);    printf("\n");}int main(){    int a[] = {65,0,95,0,0,79,82,0,84,94,86,90,0};    int n, zeronum;    n = sizeof(a) / sizeof(a[0]);    printArray(a, n);    zeronum = RemoveZeroElements(a, n);    printArray(a, n - zeronum);    printf("zreo numbers: %d\n", zeronum);    return 0;}

10. Find the minimum and maximum values in a group of input numbers.

#include<stdio.h>#include<stdbool.h>#include<math.h>void solve(){    double n, min, max;    printf("Enter the elements of the array, one per line.\n");    printf("use -1 to sigal the end of the list.\n");    printf("? ");    scanf("%lf", &n);    max = min = n;    printf("? ");    while(~scanf("%lf", &n) && (n != -1)) {        if(n < min)            min = n;        if(max < n)            max = n;        printf("? ");    }    printf("The range of value is %lf-%lf\n", min, max);}int main(){    solve();    return 0;}

11. dynamically allocate an integer array and assign values as required

#include<stdio.h>#include<stdbool.h>#include<math.h>#include<stdlib.h>int *indexArray(int n){    int *p = (int *)malloc(n * sizeof(int));    for(int i = 0; i < n; i++)        p[i] = i;    return p;}int main(){    int *ip, n;    scanf("%d", &n);    ip = indexArray(n);    for(int i = 0; i < n; i++)        printf("%d ", ip[i]);    printf("\n");    free(ip);    return 0;}

 

 

Related Article

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.