0% found this document useful (0 votes)
84 views1 page

Array Sum and Sorting Example

The document shows code to calculate the sum of elements in an array and then sort the elements of an array in ascending order by swapping adjacent elements.

Uploaded by

menrie luz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views1 page

Array Sum and Sorting Example

The document shows code to calculate the sum of elements in an array and then sort the elements of an array in ascending order by swapping adjacent elements.

Uploaded by

menrie luz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

static void Main(string[] args)

{
//Sum of all elements in an array
int[] array=new int []{2,2,2,2};
int sum1 = [Link]();
[Link]("The sum is: {0}", sum1);
[Link]();
}

//Ascending the elements of the array


using System;
using [Link];
using [Link];
using [Link];

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] a = new int[5];
int tmp, j;
[Link]("Enter the element of the array:");
for (int i = 0; i < [Link]; i++)
{
[Link]("Element {0};",i);
a[i] = Convert.ToInt32([Link]());
}
for (int i = 0; i < [Link]; i++)
{
for ( j = i + 1; j < [Link]; j++)
{
if (a[j] < a[i])
{
tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
[Link]("{0} {1} {2} {3} {4}", a[0],
a[1],a[2],a[3],a[4]);
[Link]();
}
}
}

You might also like