1.11M Members

Largest odd number in a array help

 
0
 

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

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

I didn't really bother trying to debug the above. I would make sure that you are reading in your values correctly from your file (print them out after the read). Otherwise the following works:

//assuming largest was set to zero
void largestOdd(int *B, int sizeB, int& largest){
for(int i = 0; i < sizeB; i++)
if( ( (B % 2) == 1) && (B > largest) )
largest = B;
}

int main(){
int number[] = {1,2,3,4,9,8,7,6,5}, largest = 0;
largestOdd(number, sizeof(number), largest);
cout << largest << endl;
return(0);
}

 
0
 

Just be careful with the sizeof(), it can return bad values sometimes. Instead use the the value returned as the number of bytes or values read from the file to correctly index through the array.

 
0
 

Hey, thanks for the suggestions but they didnt work :(

 
0
 

Yeah I just noticed after I sent the reply that sizeof() was acting wacky, it sometimes returned the value 10, but sometimes it didn't. That code runs and works fine though.

 
0
 

can u test it for me if u dont mind? and post the code u use with ur changes?

 
0
 

attach your number file on your original post

 
0
 

umm i dont know how to attach the file but here r the numbers i had in my data.txt file

30
55
39
290
398
96
900
10
33
22
35
46
67
35
55
555
456
23
432
100
30
90
808
898
999
80
25
321
215
276
27
89
40
80
90
356
89
201
214
973
12
53
289
80
799
68
89
854
678
981

 
0
 

ok, so I can't run your program as I thought because I do not have the header conio.h in linux. This is a simple problem though. You need to as I mentioned earlier make sure that you are reading the values in from your file correctly by first reading them in and then printing them out. Then you have file reading basics down. Once that is solved, if it needed solving, then just use the function I gave you.

void largestOdd(int *B, int sizeB, int& largest)

So your code would look like:

void largestOdd(int *B, int sizeB, int& largest){
  for(int i = 0; i < sizeB; i++)
    if( ( (B[i] % 2) == 1) && (B[i] > largest) )
      largest = B[i];
}

int 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, 50, Largest); //function call

	cout << "\nThe largest odd number is: " << Largest;
	
getch();
ins.close();
return(0);
}


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;
}

-Sam

 
0
 

Okay thanks! I'll give it a shot when I get home and let u onow

 
0
 

On a hunch I decided to find out how to read numbers from a text file, as that is something I never had to do before. This reads data.txt in two different ways and prints the largest odd number. Usually you write data to files from a program and it is saved as a non-readable text file that you can just read directly back into an array, which is easier to do than read from a text formated file.

-Sam

#include <iostream>
#include <fstream> 
#include <sstream> 

#define FILE_LOC "/home/sam/Desktop/data.txt"

using namespace std;


//assuming largest was set to zero
void largestOdd(int *B, int sizeB, int& largest){
  for(int i = 0; i < sizeB; i++)
    if( ( (B[i] % 2) == 1) && (B[i] > largest) )
      largest = B[i];
}


int nReadFile1(int *Array){
  ifstream fin(FILE_LOC);
  string line;
  int val, i = 0;

  while( getline(fin, line) ) {
    //cout << line << endl;
    istringstream tokenizer(line);
    string token;

    //this line removes the values seperated by cariage returns
    getline(tokenizer, token, (char)(13) );
    istringstream int_iss(token);
    int_iss >> val;
    printf("%d\n", val);
    Array[i] = val;
    i++;
  }
 
  return(i);
}


int nReadFile2(int *Array) {
  int i = 0, val;
  ifstream inFile;
    
  inFile.open(FILE_LOC, ifstream::in);
  if(!inFile){
    printf("File open error\n");
    return(-1);
  }
    
  while (inFile >> val) {
    printf("%d\n", val);
    Array[i] = val;
    i++;
  }
    
  inFile.close(); 
  return(i);
}


int main(){
  int x[50]; //array to hold 50 elements
  int Size, Largest = 0;

  //Size = nReadFile1(x);
  Size = nReadFile2(x);

  printf("\nFile contained %d integers\n", Size);

  largestOdd(x, Size, Largest); //function call

  printf("\nThe largest odd number is: %d\n", Largest);
	
return(0);
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article