My gut tells me I am doing something wrong here, but I am stumped.
This program runs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw
int const MAXINDEX = 50;
using namespace std;
int main(int argc, const char *argv[])
{
double alpha[MAXINDEX];
// Giving lower half of array values
for (int i = 0; i < 24; i++) {
alpha[i] = sqrt(i);
}
// Giving upper half of array values
for (int i = 25; i < MAXINDEX; i++) {
alpha[i] = i * 3;
}
int largestIndex = 0;
for (int i = 0; i < MAXINDEX; i++) {
if (alpha[largestIndex] < alpha[i]) {
largestIndex = i;
}
}
cout << "Largest index in alpha is: " << alpha[largestIndex] << " at postion " << largestIndex << endl;
return 0;
}
|
output is "correct" or as expected
|
Largest index in alpha is: 147 at postion 49
|
but.
When this program runs (moved last code block as a function)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw
int const MAXINDEX = 50;
using namespace std;
void largestIndex(const double alpha[]);
int main(int argc, const char *argv[])
{
double alpha[MAXINDEX];
// Giving lower half of array values
for (int i = 0; i < 24; i++) {
alpha[i] = sqrt(i);
}
// Giving upper half of array values
for (int i = 25; i < MAXINDEX; i++) {
alpha[i] = i * 3;
}
largestIndex(&alpha[MAXINDEX]);
return 0;
}
void largestIndex(const double alpha[]){
int largestIndex = 0;
for (int i = 0; i < MAXINDEX; i++) {
if (alpha[largestIndex] < alpha[i]) {
largestIndex = i;
}
}
cout << "Largest index in alpha is: " << alpha[largestIndex] << " at postion " << largestIndex << endl;
}
|
output erroneous / unexpected
|
Largest index in alpha is: 3.06837e+257 at postion 48
|
Can someone help me deconstruct what is going wrong here?
I Think the issue is with the de-reference? As arrays can only be passed by reference. I am having trouble compiling the program unless I add the "&" to
1 2
|
largestIndex(&alpha[MAXINDEX]);
|
Another layer to my confusion is
I just recently wrote this...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#include <iostream>
#include <math.h> /* sqrt */
#include <iomanip> // std::setw
int const MAXINDEX = 50;
using namespace std;
int smallestIndex(const double array[]);
int main(int argc, const char *argv[])
{
double alpha[MAXINDEX];
// Giving lower half of array values
for (int i = 0; i < 24; i++) {
alpha[i] = sqrt(i);
}
// Giving upper half of array values
for (int i = 25; i < MAXINDEX; i++) {
alpha[i] = i * 3;
}
// Print Table
cout << setprecision(7);
int k = 1;
for (int j = 0; j < MAXINDEX; j++, k++) {
cout << setw(8) << alpha[j] << " ";
if (k == 10) {
cout << endl;
k = 0;
}
}
int smindx = smallestIndex(&alpha[MAXINDEX]);
cout << "Smallest index in alpha is: " << alpha[smindx] << " at postion " << smindx << endl;
return 0;
}
// Smallest index
int smallestIndex(const double alpha[MAXINDEX])
{
int index, smindx = 0;
for (index = 0; index < MAXINDEX; index++) {
if (alpha[index] < alpha[smindx]) {
smindx = index;
}
}
return smindx;
}
|
And the above program does not seem to have an issue with the de-reference.
So I am confused and want to understand my error.
The more "functional" version. e.g one function.