asp.net C # array usage

Source: Internet
Author: User

Type[] Typename=new type[size];

such as int[] a=new int[2];string[] str=new string[5];

In fact, we usually use int[],string[] ... , we've created an array of arrays, but we don't usually have that kind of consciousness.

(1): Type data types can not be missing, and to unify, and can not be as int[] a=new array[];

(2): The size of the array can not be missing, or C # is considered an error, because the array is a fixed length of memory;

(3): The right side is a bracket [], not ()

Look at a simple example

#include <iostream>
using namespace Std;

int main ()
{
int sample[10]; This reserves an integer elements
int t;

Load the array
for (t=0; t<10; ++t) sample[t] = t;

Display the array
for (t=0; t<10; ++t)
cout << "This is sample[" << t << "]:" << sample[t] << "n";

return 0;
}

Output

This is sample[0]: 0
This is sample[1]: 1
This is sample[2]: 2
This is sample[3]: 3
This is sample[4]: 4
This is sample[5]: 5
This is sample[6]: 6
This is sample[7]: 7
This is sample[8]: 8
This is sample[9]: 9

To find the average of an array

#include <iostream>
using namespace Std;

int main ()
{
int i, AVG, Min_val, Max_val;
int nums[10];

Nums[0] = 13;
NUMS[1] = 18;
NUMS[2] = 75;
NUMS[3] = 120;
NUMS[4] = 1321;
NUMS[5] = 56;
NUMS[6] = 13124;
NUMS[7] = 12123;
NUMS[8] =-19312;
NUMS[9] = 88123;

Compute the average
Avg = 0;
for (i=0; i<10; i++)
AVG = Nums[i];

Avg/= 10;

cout << "Average is" << avg << ' n ';

return 0;
}

Average is 9566


Find the maximum and minimum values in the data

#include <iostream>
using namespace Std;

int main ()
{
int i, AVG, Min_val, Max_val;
int nums[10];

Nums[0] = 13;
NUMS[1] = 18;
NUMS[2] = 75;
NUMS[3] = 120;
NUMS[4] = 1321;
NUMS[5] = 56;
NUMS[6] = 13124;
NUMS[7] = 12123;
NUMS[8] =-19312;
NUMS[9] = 88123;


Find minimum and maximum values
Min_val = Max_val = Nums[0];
For (I=1 i<10; i++) {
if (Nums[i] < min_val) Min_val = Nums[i];
if (Nums[i] > Max_val) max_val = Nums[i];
}

cout << "Minimum value:" << min_val << ' n ';
cout << "Maximum value:" << max_val << ' n ';

return 0;
}

Minimum Value:-19312
Maximum value:88123

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.