Call between functions (c), switch case

Source: Internet
Author: User
Tags case statement switch case

Problem Description:

write the following four functions:

Init ();//design function initialization array is 1 2 3 4 5 6 7 8 9 10

Sort ();//design a sort function to implement an array in descending order: 10 9 8 7 6 5 4 3 2 1
Empty ();//empty array, all 0

Show ();//display array

Program Analysis:

the idea of the program: A. First write the first and last of the program, two parameters to be processed in the main function (an array of arr, and the length of the group Len). B. Encapsulate the several functions, plus a menu function. C. Invoke these functions with the switch case statement in the main function.

The code is as follows:

/**init ();//design function initialization array is 1 2 3 4 5 6 7 8 9 10sort ();//design sorting function, implement the descending arrangement of the array: 9 8 7 6 5 4 3 2 1empty ();//empty array, all 0show ();//Display Array * * /#include <stdio.h> #define LENGTH 10//macro defines the size of the array/*** initialize the array ****/void init (int arr[],int n) {int i = 0;/** assigns values to each element in the array * * /for (i = 0;i < n;i++) {arr[i] = i+1;}}     /*** Select sort ****/void selection_sort (int arr[],int n) {int i = 0;   Use subscript I to represent the current element in the array int j = 0;   Subscript J indicates the subscript int k of the largest element found in this cycle;   Use subscript k to store the subscript int temp of the largest element in each cycle; Define the intermediate variable used for the following interchange for (i = 0;i<n;i++) {k = i;//) Assuming that the maximum value in each loop is the value of the first value in the loop for (j = i+1;j < n;j++)//From the second start of each loop with the assumed maximum value { if (Arr[j] > Arr[k])//When finding a larger than assumed value, assign the maximum subscript to k{k = j;}} if (k! = i)//after lookup if the maximum value in the loop is not the assumed value {temp = Arr[i];//interchange, so that the value of K subscript corresponds to the maximum value in each loop arr[i] = arr[k];arr[k] = temp;}}}  /*** Bubble sort ****/void bubble_sort (int arr[],int n) {int i = 0;//define outer loop variable I and initialize to 0int j = 0;//define inner loop variable J and initialize to 0int temp; Define intermediate variables for the subsequent interchange for (i = 0;i<n-1;i++)//outer loop, a total of n-1 22 comparison {for (j = 0;j < n-1-i;j++)//inner loop, each loop compared n-1-i times {if (arr[ J+1]&GT;ARR[J])//When the adjacent 22 comparison, the latter is greater than the former, exchange {temp = arr[J+1];ARR[J+1] = arr[j];arr[j] = temp;}}}} /*** empties the array ****/void empty (int arr[],int n)//This function resets all elements in the array to 0{int i = 0;for (i = 0;i < n;i++) {Arr[i] = 0;}} /*** Print display array ****/void Show (int arr[],int n)//This function is mainly printed to show the result {int i = 0; for (i = 0;i < n;i++) {printf ("%d", AR) after each call to other functions R[i]);} printf ("\ n");} /*** Settings menu ****/void menus ()//write menu to provide users with information on how to use {printf ("******* Initialization--->1********\n");p rintf ("******* bubble sort--->2 \ n ");p rintf (" ******* Select Sort--->3******\n ");p rintf (" ******** Empty--->4*********\n ");p rintf (" ******** Print--- >5*********\n ");}  int main () {int j = 0;int a[10];  Defines an array of size 10*sizeof (int) with an int len = sizeof (a)/sizeof (a[0]);  The size of the array int select;  Define select as the parameter of the switch function int i = 1;   Define and initialize the loop variable while (i) {menu (); Call the menu function printf ("Please enter function number: \ n"), scanf ("%d", &select), switch (SELECT)//write switch function {case 1:init (A,len); break;case 2:bubble_sort (A,len); Break;case 3:selection_sort (A,len); Break;case 4:empty (A,len); Break;case 5:show (A,len); break; Default:break;}} return 0;}




Call between functions (c), switch case

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.