Getting Started with arrays

Source: Internet
Author: User

How to store 10 students ' math, Chinese and English scores in the program? Define 30 integer variables? This is too much trouble, and learning the array after the change is much simpler, here we first from the simplest array of learning.

Define an integer variable so that you can write int A;

Defines an integer array that can be written as an int a[3]; The brackets are the identifiers of the arrays, where an array of length 3 is defined as 3 variables.

So what does each variable say in the program? C language in the form of a number to represent each of the variables in the array, note that the number is starting from 0, the A array is numbered 0,1,2 variables, such as Char b[5] in the B array there is a variable numbered 0,1,2,3,4, remember that there is never an array length variable, for example Float C[10] Array has a length of 10, there is no variable numbered 10 in the C array, and the number is exactly 10 numbers from 0 to 9.

While it is convenient for us to define a set of variables at once, you need to manipulate each variable one by one in programming, which requires that we use numbers to manipulate the variables in the array, see the code below.

int a[3];

a[1]=20;//the integer 20 into a variable numbered 1 in the array

scanf ("%d", &a[0]);//Enter an integer from the keyboard into the variable numbered 0 in the array

printf ("%d", a[2]);//outputs the value in the variable with number 2 in the array

Do you understand, in fact, we used to operate a variable, and now the operation is a variable, except that the variable in the array does not have its own variable name, but by the array name plus the number of the form to represent.

The following code is used to describe the math scores of 10 students entered from the keyboard and saved to the array

Float a[10];//is used to store the math scores of 10 students

scanf ("%d", &a[0]);

scanf ("%d", &a[1]);

scanf ("%d", &a[2]);

......

scanf ("%d", &a[9]);

Now I'm telling you, the position of the array number can be a variable, for example, int i=2;a[i] Of course it represents a variable numbered 2 in a array, how can you use loops to simplify the above code?

Array Exercise: A magazine organized a reader SMS activity, selected 10 mobile phone numbers as a winning reader, the user from the keyboard input their mobile phone number query whether to win.

The first step: we have to store these 10 mobile phone numbers in our program, an array of integers just can meet our requirements.

int a[10]={ 15112345678,13012345678,13112345678,13012345678,13011111111,13822222222,18000000000,13522222222,15122222222,18922222222 };

In this way a array of 10 mobile phone numbers stored in the winning

Step two: Ask the user to enter their mobile phone number from the keyboard

int no;//to store the phone number that the user entered from the keyboard

printf ("Thank you for your participation in this event, please enter your mobile phone number query whether to win: \ n");

scanf ("%d", &no);

The third step: we just use no and the array of mobile phone numbers to find out is the winning, did not find is not winning.

if (no==a[0]| | no==a[1]| | no==a[2]| | no==a[3]| | no==a[4]| | no==a[5]| | no==a[6]| | no==a[7]| | no==a[8]| | NO==A[9])

{

printf ("Congratulations on winning the prize, please go to the designated place to collect the prizes!\n");

}

Else

{

printf ("Thank you for participating \ n");

}

The complete code is as follows:

Main ()

{

A array of 10 mobile phone numbers stored in the jackpot

int a[10]={ 15112345678,13012345678,13112345678,13012345678,13011111111,13822222222,18000000000,13522222222,15122222222,18922222222 };

int no;//to store the phone number that the user entered from the keyboard

printf ("Thank you for your participation in this event, please enter your mobile phone number query whether to win: \ n");

scanf ("%d", &no);

if (no==a[0]| | no==a[1]| | no==a[2]| | no==a[3]| | no==a[4]| | no==a[5]| | no==a[6]| | no==a[7]| | no==a[8]| | NO==A[9])

{

printf ("Congratulations on winning the prize, please go to the designated place to collect the prizes!\n");

}

Else

{

printf ("Thank you for participating \ n");

}

}

This program is not a problem, but the third step on 10 mobile phone number can also be written over, if it is 100 mobile phone number, so we can use the loop to improve a

Main ()

{

A array of 10 mobile phone numbers stored in the jackpot

int a[10]={ 15112345678,13012345678,13112345678,13012345678,13011111111,13822222222,18000000000,13522222222,15122222222,18922222222 };

The int i;//is used to control the loop, while representing the array subscript

int no;//to store the phone number that the user entered from the keyboard

printf ("Thank you for your participation in this event, please enter your mobile phone number query whether to win: \ n");

scanf ("%d", &no);

Use the loop to access the elements in the array, if the first element in the array is the same as the user entered the mobile phone number, this mobile number is winning

for (i=0;i<10;i++)

{

if (No==a[i])

{

printf ("Congratulations on winning the prize, please go to the designated place to collect the prizes!\n");

break;//now that you have found it, you don't have to keep looking, just exit the loop.

}

}

}

The above code can only output the winning situation, if the input does not win the mobile phone number program will not output, give you to leave a practice to see what code to judge not to win and output thanks to participate, now is the input not winning mobile phone number program No output directly ended, such as.

Getting Started with arrays

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.