To traverse an array in five different ways

Source: Internet
Author: User
Tags int size

To traverse an array in five different ways

#include <iostream>

using namespace Std;

int main ()

{

int sum[5]={0};

int iarray[]={1,2,3,4,5,6,7,8,9,10}; A new method of finding the length of an array, where IArray represents the beginning of the array,

int size=sizeof (IArray)/sizeof (*iarray); So sizeof (IArray) represents the length of this array, and sizeof (* (iarray+0)) represents the length of the first space

int *iptr=iarray;

int n;

for (n=0;n<size;n++)

{

Sum[1]+=iarray[n];

}

for (n=0;n<size;n++)

{

sum[0]+=* (Iarray+n);

}

for (n=0;n<size;n++)

{

Sum[2]+=iptr[n];

}

for (n=0;n<size;n++)

{

sum[3]+=* (Iptr+n);

}

for (n=0;n<size;n++)

{

sum[4]+=*iptr++;

}

for (n=0;n<5;n++)

{

cout<<sum[n]<<endl;

}

return 0;

}





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.