C Language Learning _ test1

Source: Internet
Author: User

1. Input 5 numbers on the keyboard to output the largest and smallest elements and their subscript.

# Include

Int main (void)
{
Int I, j, k, max, min;
Int a [10] = {0 };
Printf ("input number :");
For (I = 0; I <5; I ++)
Scanf ("% d", & a [I]);
Max = min = a [0];
J = k = 0;


For (I = 0; I <5; I ++)
{
If (max <a [I])
{
Max = a [I];
J = I;
}
Else if (min> a [I])
{
Min = a [I];
K = I;
}
}
Printf ("% d \ n", j, max, k, min );
Return 0;
}

2. Enter 16 integers on the keyboard and save them to the two-dimensional array 4*4 to output the elements of even rows and even columns.

# Include


Int main (void)
{
Int a [5] [5] = {0 };
Int I, j;
Printf ("input numbers :");
For (I = 0; I <4; I ++)
For (j = 0; j <4; j ++)
Scanf ("% d", & a [I] [j]);
For (I = 0; I <4; I ++)
For (j = 0; j <4; j ++)
{
If (I % 2 = 0 & j % 2 = 0)
Printf ("% d \ n", a [I] [j]);
}


Return 0;
}

3. In an ordered array, enter a number. If this number exists, the output number and its position are displayed. If this number does not exist, the output information is displayed.

# Include
# Define M 10


Int main (void)
{
Int a [M] = };
Int n; // The number of queries.
Int low, high, mid;
Int found;
Low = 0;
High = M-1;
Found = 0;
Printf ("input n :");
Scanf ("% d", & n );
While (low <= high)
{
Mid = (low + high)/2;
If (n = a [mid])
{
Found = 1;
Break;
}
Else if (n <a [mid])
High = mid-1;
Else
Low = mid + 1;
}
If (found = 1)
Printf ("index of % d is % d", n, mid );
Else
Printf ("there is not % d", n );
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.