"C Language and program design" Practice Item 26 function of implementing basic operation of linear table

Source: Internet
Author: User



"Item 1-inserting data in an ordered array (function version)"

/* * Copyright (c), CSDN College * All rights reserved. * File Name: "Item 1-insert data in an ordered array (function version)". cpp * Author: Zhang Yian * Completion Date: September 9, 2016 * version number: v1.0 * * Problem Description: There is a large enough "big" array A, which has stored n The data in ascending order. Call the function Insert (A, n, m), * You can insert several m into the appropriate position in a so that it remains in ascending order, and the return value is the number of valid digits in the array (that is, the original n plus 1). * For example, if the data in a is 1 7 8 101,n=9, the number m to be inserted is 50, and after the function insert (A, n, m) is called, the data in a is 1 7 8, and the 101,n=10. */#include <stdio.h> #define N 100int Insert (int[], int, int); int main () {int A[n]={1,7,8,17,23,24,59,62,101};int i , N=9;int m=50;n = insert (a,n,m), for (i=0;i<n;i++) {printf ("%d\t", A[i]);} printf ("\ n"); return 0; }/* The function of the insert (A,N,M) is to insert a number into the order array in the entry parameter: A-array of the number of the number of n-digits of the number m-inserted digit */int insert (int a[],int n,int m) {int i=n-1;//first Reduce the number of arrays by one because it is starting from 0 a[0]=1 while (a[i]>m&&i>=0)//When the number is greater than 0 and the value in the array is greater than m when the value in the {a[i+1]=a[i];//array is back to a i--;} i++;/  /because without adding one, the value assigned to the a[i]=m;//m on 24 assigns the array N++;//10 a number return n; }//experience, when you can not understand a program. The best way to do this is to think of yourself as a computer. Follow the steps from top to bottom step-by-step execution, or step-by-step debugging for easy comprehension
Run results

"Item 2-Deleting an array element"

   /* * Copyright (c), CSDN College   * All rights reserved.   * File Name: "Item 2-delete array element". cpp   *    : Zhang Yian   * Completion Date: September 9, 2016   * Version number: v1.0   *   * Problem Description: The function of the Del function is to delete the specified element in array a Element X,n is the number of elements in array a. The return value of the function, which is the number of valid elements after the element is deleted (there may be duplicate elements in the array). The prototype of the function is: int del (int a[10],int n,int x) (1) Implement this function and complete the test. (2) If the elements in the array are required to be in ascending order when the function is called? */#include <stdio.h>int del (int a[],int n,int x); int main () {int a[20]={ 12,45,78,47,58,25,36,98,56,25,41,45,48,42,35,59,75,41,24,86};int i,n;n = del (a,20,75); for (i=0;i<n;i++) {printf ( "%d\n", A[i]);} printf ("\ n"); return 0; }  /* del (int a[],int n,int x) function is for an element in a coral array input parameter    a[]-array element n array number x to delete the number  */int del (int a[],int n,int x) { int P,q=0;for (p=0;p<n;p++) {if (a[q]!=x) {A[p]=a[q];p + +;} return p;//I can't figure it out, why give P back to the main function will output a different dismissal, is it changing the array? }
Run results



"Item 3-sort of array"

/* * Copyright (c), CSDN College * All rights reserved. * File Name: "Item 4-sort of wages". cpp * Author: Zhang Yian * Completion Date: September 9, 2016 * version number: v1.0 * * Problem Description: Read the worker's salary (no more than 500 people) from the file Salary.txt, all Add 20% (a good thing), then sort the payroll data and save the sorted results to the file ordered_salary.txt. Finish opening the file to see the results, play so much data between the palm, I believe you will have a sense of accomplishment. */#include <stdio.h>void bubble_sort (int arr[],int num), void output_array (int arr[],int num); int main () {int a[20] ={87,76,62,58,77,85,92,88,77,67,80,68,88,87,64,59,61,76,75,63};int b[15]={ 27,61,49,88,4,20,28,31,42,62,64,14,88,27,73};bubble_sort (a,20); Output_array (a,20); Bubble_sort (b,15); output_ Array (b,15); return 0; } void bibble_sort (int arr[],int num) {int i,j,t;for (j=0;j<num-1;j++)//num-1 trip comparison for (i=0;i<num-j-1;i++)//    Num-j times per trip 22 comparison if (Arr[i]<arr[i+1]) {t=arr[i];    ARR[I]=ARR[I+1]; arr[i+1]=t;} return;} void Output_array (int arr[],int num) {int i;for (i=0;i<num-1;i++) {printf ("%d", Arr[i]);} printf ("%d\n", Arr[num-1]); return; }//again this algorithm, to write down, do it all the time I can't remember!
Run results




"Item 4-Search Results"

/* * Copyright (c), CSDN College * All rights reserved. * File Name: "Item 4-sort of wages". cpp * Author: Zhang Yian * Completion Date: September 9, 2016 * version number: v1.0 * * Problem Description: Score1.txt provides data for a university's two classes of tests, including each student Number and results of the study. * Please compile the program, enter the student number, find out the student's achievements. * Tip 1: You can define two arrays of int, where one n holds the student number, the other s holds the score, can guarantee two arrays, the element subscript is the same, corresponding to the same classmate. For example n[18] The value is 3123,s[18] is 98, the student number of 3123 students score is 98. * Tip 2: Because the data is unordered, using the sequential lookup algorithm, in the N array based on the number of the search, in the s array corresponding to the underlying value is its score. For example, by finding in N, the student with the number 3123 is subscript 18, then its score is s[18]. * Below is part of the code to complete the application, which has been able to output a list of accomplishments.  Please implement the relevant functions on this basis: */#include <stdio.h> #include <stdlib.h>int readdata (int[], int []);  int search (int[], int, int); int main () {int Num[200],score[200];int count;int index;int key;count = ReadData (Num,score); scanf ("%d", &key); index = Search (Num,count,key), if (index>=count) printf ("The classmate with no student number%d", key), Else printf ("The grade of the student with the number%d is:%d\n", Key,score    [index]); return 0; } int readdata (int n[],int s[]) {int c=0; FILE *FP;FP = fopen ("Scorel.txt", "R"), if (fp==null) {printf ("Open File error!\n"); exit (0);} Routines while (FSCANF (FP, "%d%d", &n[c],&s[c])!=eof) {C + +;} fclose (FP); return c;} int search (int n[],int c,int k) {int i;for (i=0;i<c;i++) {if (n[i]==k) break;}    if (i==c) i=-1; return i;}
Run results



"Item 4-Search results"

"C Language and program design" Practice Item 26 function of implementing basic operation of linear table

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.