我写了一些代码,打印3到7之间的25个随机数,并将它们放入数组中,然后放入数组中。现在我将如何减去数组中的第一个数字减去数组中的最后一个数字?这是我的代码到目前为止。我有函数调用和原型制作;只是不知道要放什么东西在定义完全相同:第一个减去数组中的最后一个元素
#include <time.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
// Function prototypes
void showArray (int a[ ], int size); // shows the array in the format "int a [ ] = { 3, 7, 4, ... ,5, 6, 3, 4, 7 } "
void showBeforeIndex(int a [ ], int size, int index); // shows all array values before a specified index
int firstMinusLast (int a[ ], int size);
// **************************************************************************************************************************************
int main()
{
// Array and reverse array
srand((int)time(NULL));
int i=0;
const int SIZE = 25;
int randvalue[SIZE];
cout << "Making an array of 25 random integers from 3 to 7!" << endl;
for(; i < SIZE; i++)
{
randvalue[i] = rand() % 5 + 3; // random number between 3 to 7
}
cout << "Original array a [ ] = {";
showArray(randvalue, SIZE);
cout << "}" << endl;
int j = SIZE-1;
i = 0;
while(i <= j)
{
swap(randvalue[i], randvalue[j]);
i++;
j--;
}
cout << "Reversed array a [ ] = {";
showArray(randvalue, SIZE);
cout << "}" << endl;
// *******************************************************************************************************
// Function call for FIRST - LAST
int returnFirstLast = firstMinusLast (randvalue, 25);
cout << "The difference between the first and and last array elements is " << returnFirstLast << endl;
//********************************************************************************************************
return 0;
}
// Function definition for ARRAY
void showArray (int a[ ], int size)
{
int sum = 0;
for(int i = 0; i < size; i++)
cout << a[i];
}
// Function definition for FIRST - LAST
int firstMinusLast (int a[ ], int size)
{
int fml;
return fml;
}
为什么不是fml = a [0] -a [size-1]? – aichao