One Dimensional array

#include "stdafx.h"
#include <iostream>
using namespace std;

void initializeArray(int A[], int size);
void fillArray(int A[], int size);
float avgArray(const int A[], int size);
int largestElement(const int A[], int size);
int smallestElement(const int A[], int size);

int main()
{
const int size = 50;
int array[size] = { 0 };

cout << "Enter " << size << " integers: " << endl;
fillArray(array, size);
cout << "The average of the elements: " << avgArray(array, size) << endl;
cout << "The largest element in is: " << array[largestElement(array, size)] << endl;
cout << "The smallest element in is: " << array[smallestElement(array, size)] << endl;
system("pause");
return 0;
}

void initializeArray(int A[], int size)
{
for (int index = 0; index < size; index++)
A[index] = 0;
}
void fillArray(int A[], int size)
{
for (int index = 0; index < size; index++)
{
cout << "Enter A[" << index << "]: ";
cin >> A[index];
}
}

float avgArray(const int A[], int size)
{
float avg;
int sum = 0;
for (int index = 0; index < size; index++)
sum = sum + A[index];
avg = sum / size;
return avg;
}

int largestElement(const int A[], int size)
{
int maxIndex = 0;
for (int index = 1; index < size; index++)
if (A[maxIndex] < A[index])
maxIndex = index;
return maxIndex;
}

int smallestElement(const int A[], int size)
{
int minIndex = 0;
for (int index = 1; index < size; index++)
if (A[minIndex] > A[index])
minIndex = index;
return minIndex;
}

No comments:

Post a Comment