2016-07-28 19 views
0

Ich habe einen Code geschrieben, der 25 zufällige Zahlen zwischen 3 und 7 ausgibt und sie in ein Array und dann in sein umgekehrtes Array legt. Wie würde ich jetzt die erste Zahl im Array abzüglich der letzten Zahl im Array subtrahieren? Hier ist mein Code soweit. Ich habe die Funktion Aufruf und Prototyp gemacht; nur nicht sicher, was in der Definition setzen genau:Erstes minus letztes Element in einem Array

#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; 
} 
+0

Warum nicht fml = a [0] -a [Größe-1]? – aichao

Antwort

-1

Wenn Sie bereits die Größe des Arrays, und Sie wissen das Array ist voll bestückt:

int firstMinusLast (int a[ ], int size){ 
     return a[0] - a[size - 1]; 
    } 
-1

In C/C++ Arrays Indexed beginnend mit 0. So ist das erste Element auf Index 0. Da das erste Element eines Arrays auf Index 0 ist, dann ist das letzte Element eines Arrays auf einen Index gleich der Größe des Arrays minus 1. So code:

Das erste Element ist a[0]

Das letzte Element ist a[SIZE - 1]

So ihre Differenz zu erhalten: fml Sie einfach schreiben: fml = a[0] - a[SIZE - 1]

Diese für eine Funktion ziemlich einfach scheint so vielleicht werden Sie etwas größer oder anders zu erwarten.

Benötigen Sie den absoluten Wert der Differenz? Das Ausmaß der Veränderung ohne das Zeichen? Wenn ja, benutzen Sie einfach die Absolutwertfunktion.

fml = abs(a[0] - a[SIZE-1]);

Wenn Sie sagen wollen, dass Sie zuerst Minus vor der Rückseite zuletzt wollen, dann ist dies einfach tun:

fml = a[SIZE-1] - a[0];

Wenn es den abs braucht dann spielt es keine Rolle, welche wie du den subtrahieren kannst.

Verwandte Themen