Hi so I have this program to find the largest odd number from an array of 50, the program runs fine, but the problem is after reading the numbers from file it says the largest odd number is 0. Can some one please have a look for me and tell me where my mistake is. ITS DRIVING ME INSANE haha!
//File: lab8a.cpp
/*Purpose: to find the largest odd number in a 50-element
array of integers.*/
#include <iostream>
#include <fstream>
#include <conio.h>
#define in_file "data.txt"
using namespace std;
ifstream ins; //ins as input stream
void readValues(int[],int); //input values into array
void largestOdd(int[],int&); //find largest odd number
void main()
{
ins.open(in_file); //open file
int x[50]; //array to hold 50 elements
const int Size = 50;
int Largest = 0;
readValues(x,Size); //function call
largestOdd(x,Largest); //function call
cout << "\nThe largest odd number is: " << Largest;
getch();
ins.close();
}
void readValues(int A[], int sizeA)
{
char next_char;
int i = 0;
for(;;)
{
if (ins.eof() || i>=sizeA) //check end of file
break;
ins >> A[i];
cout << A[i] << endl; //echo print input data
ins.get(next_char); //skipping end-of-line char
i++;
}
return;
}
void largestOdd(int B[50], int& largest)
{
int i = 0;
ins >> B[i];
while(!ins.eof())
{
if(B[i] % 2 == 1)
{
if(B[i] > B[i+1])
{
largest = B[i];
B[i] = B[i+1];
B[i+1] = largest;
}
}
i++;
}
}