ArrayExample1

// arrays example
#include <iostream>
usingnamespace std;

int billy [] = {16, 2, 77, 40, 12071};
int n, result=0;

int main ()
{
for ( n=0 ; n<5 ; n++ )
{
result += billy[n];
}
cout << result;
return 0;
}




// arrays/readsum.cpp -- Read numbers into
// an array and sum them.
// Fred Swartz - 2003-11-20

#include <iostream>
using namespace std;

int main() {
int a[1000]; // Declare an array of 1000 ints
int n = 0; // Number of values in a.

while (cin >> a[n]) {
n++;
}

int sum = 0; // Start the total sum at 0.
for (int i=0; i<n; i++) {
sum = sum + a[i]; // Add the next element to the total
}

cout << sum << endl;

return 0;
}



// arrays/reverse-input.cpp - Reverses order of numbers in input.
#include <iostream>
using namespace std;

int main() {
//--- Declare array and number of elements in it.
float a[100000];
int n; // Number of values currenlty in array.

//--- Read numbers into an array
n = 0;
while (cin >> a[n]) {
n++;
}

//--- Print array in reverse order
for (int i=n-1; i>=0; i--) {
cout << a[i] << endl;
}

return 0;
}//end main


// a program to find the smallest number in an array
// named balance, a very simple search function
#include <iostream>
using namespace std;
#define  n  10

int main()
{

int i;
float  small, balance[n]={100.00,40.00,-30.00,400.00,60.00,-25.00,-24.00,
0.00,3.24,0.50};

small = balance[0];

// loop for displaying array content....

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

cout<<balance[i]<<" ";

// another loop do the array element comparing...

for(i=1; i<n; i++)   // check until condition i = n

{

if(small > balance[i])

small = balance[i];

}

cout<<"\nSearching..."<<endl;

// display the result...

cout<<"The smallest value in the given array is = "
<<small<<endl;

return  0;

}

 


Post a Comment

0 Comments