LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2004, 07:35 AM   #1
suchi_s
Member
 
Registered: May 2004
Posts: 133

Rep: Reputation: 15
function to calculate maximum value


maximum value from the column in the file
45
34
67
89
89
90
the ans should be 90
is there any function in linux to calculate the maximum value
 
Old 10-26-2004, 07:55 AM   #2
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
I dont know of any function, but it wouldnt be too hard to parse it yourself.

Code:
Psuedocode:

        function ParseFile()
        {
                int NumberOfItems = 0;
                Parse through the file and get he number of items in the column

                return(NumberOfItems);
        }

        function main()
        {
                const ArraySize = ParseFile();

                int HighestValue = 0; //The highest value will be stored here
                int NumberArray[ArraySize]; //The array to store the column

                        for(int i=0; i=ArraySize;i++)
                        {
                                if NumberArray[i]>=HighestValue
                                {
                                        HighestValue = NumberArray[i];
                                }
                        }

                        printf("The higest value was %d",HighestValue);
                }
There are probably easier, more efficient ways of doing it but I havent had much sleep. There are probably some syntax errors there, so dont take it as working code, just a base that should help.

Chris
 
Old 10-26-2004, 08:13 AM   #3
suchi_s
Member
 
Registered: May 2004
Posts: 133

Original Poster
Rep: Reputation: 15
but how to initalise the array NumberArray as its values are stored in a file
 
Old 10-26-2004, 10:17 AM   #4
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
This code works:
Code:
#include <iostream>
#include <fstream.h>
#include <ctype.h>
using namespace std;

int parseFile(char filename[80]);
int Highest();

int parseFile(char filename[80])
{
	int intNumberOfElements = 0; //The total number of elements in the the file
	ifstream fin(filename);    // Open the file passed to this function for reading
	string temp; //Where we store the string we ouput from the file

		cout << "Getting Total Number Of Elemnents...."<<endl;
		while (getline(fin,temp))
		{	
			intNumberOfElements++; //Add one to the total
		}
		fin.close(); //Close the file
		cout << "Done"<<endl;
		
	return(intNumberOfElements);
}

int Highest()
{
	char filename[80]; //The file you want to open
	int HighestValue = 0; //This will store the largest value from the text file
	
	string temp; //Where we store the string we ouput from the file
	int i = 0; //The loop counter
	
	cout  << "File: "; //Ask for a filename
	cin >> filename;
	
	const int NumberOfElements = parseFile(filename); //Declare a constant with the number of elements in the file so we can create an array
	cout << "Elements: " << NumberOfElements << endl;  //Tell the user how many elements there are
	int intNumberArray[NumberOfElements]; //Create the array
	ifstream fin(filename);    // Open the file passed to this function for reading
	//Fill the array
	cout << "Filling Array...."<<endl;
		while (getline(fin,temp))
		{
			intNumberArray[i] = atoi(temp.c_str()); //Add the current line nto the array
			i++; //Add one to the total
		}
	cout << "Done"<<endl;
	cout << "Checking for highest value...."<<endl;
	for(i = 0; i < NumberOfElements; i++)
	{
		if(intNumberArray[i]>=HighestValue)
		{
			HighestValue=intNumberArray[i];
		}
	}
	fin.close(); //Close the file again
	cout << "Done...."<<endl;
	return(HighestValue);
}
All you need to do is include the two functions in your source files, and the right header files. To get the highest value you just need to have the following in the funtion that calls the ones to find the highest number:
Code:
int HighestNumber = Highest;
cout << HighestNumber
Hope that helps,
Chris

EDIT:
Testing this I used a file in the same directory as the executable. The file should be formatted like:
Code:
1
2
3
4
5
6
...
i.e. with each number on a new line.

Last edited by clb; 10-26-2004 at 10:19 AM.
 
Old 10-26-2004, 10:23 AM   #5
jordanGSU
Member
 
Registered: Sep 2004
Distribution: Debian, Slackware, Arch
Posts: 65

Rep: Reputation: 15
Do you need to store all the values in an array?
if not, just have a max variable thats initialized to zero...read in each value one by one....if the value if larger than the max, set max to that.
code would be considerably shorter if you could do that.
 
Old 10-26-2004, 10:38 AM   #6
clb
Member
 
Registered: Sep 2004
Location: UK
Distribution: Ubuntu
Posts: 117

Rep: Reputation: 16
Lol, yeah it would be shorter and more effiecient, it just didnt occur to me not to use an array.
It depends if you want access to each individual number afterwards or not I guess.
If you just want the highest number in the list, then use this code:
Code:
#include <iostream>
#include <fstream.h>
#include <ctype.h>
using namespace std;

int Highest();

int Highest()
{
	char filename[80]; //The file you want to open
	int HighestValue = 0; //This will store the largest value from the text file
	int tempint = 0; //This will store the converted temp variable
	string temp; //Where we store the string we ouput from the file
	cout  << "File: "; //Ask for a filename
	cin >> filename;
	ifstream fin(filename);    // Open the file passed to this function for reading
	cout << "Processing File...."<<endl;
	while (getline(fin,temp))
	{	
		tempint = atoi(temp.c_str());
		if (tempint>=HighestValue)
		{
			HighestValue=tempint;
		}
	}
	fin.close(); //Close the file again
	cout << "Done...."<<endl;
	return(HighestValue);
}
Use it in the same way as I described before.
Chris
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to calculate bandwidth on linux epsharma Linux - Software 4 07-02-2006 10:45 AM
Calculate Interface Traffic JJX Linux - Networking 7 10-22-2004 02:38 AM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
calculate traffic using iptables p_nanda2002 Linux - Software 4 11-01-2002 02:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:57 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration