PTA Array Jobs

Source: Internet
Author: User

7-2 finding the maximum value and its subscript

1. Topics

Submit Results

2. Design Ideas

Define variables, enter arrays, define initial values for Max Maxlab

Cycle comparison before and after the value, the final output

3. Flowchart

The code is as follows

#include <stdio.h>intMain () {inta[ -],i,n,max,maxlab,lab=0; scanf ("%d",&N);  for(i=0; i<n;i++) {scanf ("%d",&A[i]); } Max=A[lab]; Maxlab=Lab;  for(i=0; i<n;i++){       if(a[i]>max) {Max=A[i]; Maxlab=i; }} printf ("%d%d", Max,maxlab); return 0;} 

4. Results of the answer

Topic Two: 7-3

1. Topics

2. Submission List

3. Design ideas (flowchart)

Define sub-functions, input n, loop input array, execute sub-function, loop output result, determine whether subscript is maximum, and then output by condition

Sub-function (execution loop, one exchange per cycle A[i] a[n-1-i])

Flow chart

The code is as follows

#include <stdio.h>voidExchangeintA[],intN) {    inti,t;  for(i=0; i<n/2; i++) {T=A[i]; A[i]=a[n-1-i]; A[n-1-i]=T;} }intMain () {inta[ -],n,i; scanf ("%d",&N);  for(i=0; i<n;i++) {scanf ("%d",&A[i]);    } exchange (A,n);  for(i=0; i<n;i++){      if(i==n-1) {printf ("%d", A[i]);} Else{printf ("%d", A[i]); }        }        return 0; }

4. Submit an Answer

Topic Three: 7-4 find integers

1. Topics

The subject asks for a given x from the input n integers. If found, the position of the output x (starting from 0), if not found, outputs "not Found".

Input format:

The input gives two positive integers n (≤20) and X in the first row, and the second line gives n integers. Numbers do not exceed long integers, separated by spaces.

Output format:

Prints the position of x in a row, or "not Found".

2. Submission of results

2. Design Ideas

Define variables, input n,x, loop input arrays, look for numbers equal to x in an array and output, flag=0, and finally determine if found.

Flow chart

The code is as follows

#include <stdio.h>
int main ()
{
int i,x,n,flag=1;
scanf ("%d", &n);
scanf ("%d", &x);
int a[n];
for (i=0;i<n;i++) {
scanf ("%d", &a[i]);

if (a[i]==x) {
printf ("%d", I);
flag=0;
}
}
if (flag==1)
printf ("Not found\n");


return 0;
}

4. Results of the answer

Iv. Mutual evaluation of students

Classmate Code

My Code

3.What is the difference between my classmates and my code? What are the advantages of each? Which style of code do you prefer?

Compared to him, my code is more concise, less a sub-function, his code is more understandable, my choice is the most concise code.

Iv. Summary of this week's study

1. What did you learn this week?

Array loop output, child function invocation, and loop selection nesting.

PTA Array Jobs

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.