C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old December 12th, 2006, 05:26 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Function with array

Hi there, my problem is that,i have to read in a series of number (upto 100 numbers) from a file, and then find the minimum number, and it's location using a function, i tried and it's not working, plz help me out.
Thanks

here's the code

Code:
#include <iostream>
#include <fstream>
using namespace std;

void nMinFinder(int arnList[100]);

int main()
{

	ifstream fin;
	int arnList[100];
	fin.open("numbers.txt");
	for (int x=0;x<100;x++)
	{
	fin>>arnList[x];
	}
	nMinFinder(arnList[100]);
	cout<<nMinFinder<<endl;
	return 0;
}

void nMinFinder (int arnList[100])
{
	int nCount=0;
	int nIndex;
	int nMin=999999;
	if (arnList[nCount]<nMin)
	{
	nMin=arnList[nCount];
	nIndex=nCount;
	nCount++;
	}
	cout<<"The Minimum Number is "<<nMin<<" and the index is "<<nIndex<<endl;
}

Reply With Quote
  #2  
Old December 12th, 2006, 06:01 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Have a look through this:

Code:
#include <iostream>
#include <fstream>

using namespace std;

void nMinFinder( int arnList[], int size );

int main()
{
    const int sz = 100;
    ifstream fin;
    int arnList[sz];
    fin.open( "numbers.txt" );

    // Always check if the file was opened. If your program
    // exits with no output you may think your program is wrong.
    // When all you needed to do was put these couple of lines in.
    if ( !fin )
    {
	cerr << "Failed to locate the file." << endl;

	return 1;
    }

    for ( int x = 0; x < sz; x++ )
    {
        fin >> arnList[x];
    }

    // Pass the array name and the size of the array.
    nMinFinder( arnList, sz );

    // Commented the next line out. You are trying to output a function.
    //cout << nMinFinder << endl;
    return 0;
}

void nMinFinder( int arnList[], int size )
{
    int nCount = 0;
    int nIndex = 0;
    // Set the nMin to the first element in the array.
    int nMin = arnList[0];

    // You need a loop to go through the array.
    while ( nCount < size )
    {
        if ( arnList[nCount] < nMin )
        {
            nMin = arnList[nCount];
            nIndex = nCount;
        }

        nCount++;
    }

	cout << "The Minimum Number is " << nMin
	     << " and the index is " << nIndex << endl;
}


Hope this helps you.

Reply With Quote
  #3  
Old December 12th, 2006, 06:35 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Thanks Paul, for the help, Paul your code works, but I get a blank screen as output.

this is the code i used

Code:
#include <iostream>
#include <fstream>
using namespace std;

void nMinFinder(int arnList[],int nSize);

int main()
{

	ifstream fin;
	const int nSize=100;
	int arnList[nSize];
	fin.open("numbers.txt");
	if ( !fin )
    {
	cout << "Failed to locate the file." << endl;
	return 1;
	}
	for (int x=0;x<nSize;x++)
	{
	fin>>arnList[x];
	}
	nMinFinder(arnList,nSize);
	return 0;
}

void nMinFinder (int arnList[],int nSize)
{
	int nCount=0;
	int nIndex=0;
	int nMin=arnList[0];
	while ( nCount < nSize )
	{
	if (arnList[nCount]<nMin)
	{
	nMin=arnList[nCount];
	nIndex=nCount+1;
	nCount++;
	}
	}
	cout<<"The Minimum Number is "<<nMin<<" and the index is "<<nIndex<<endl;
}


so Paul, thanks once again, and plz help me solve this.
Take care.

Reply With Quote
  #4  
Old December 12th, 2006, 07:30 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
You have your nCount++ in the wrong place.

Code:
while ( nCount < nSize )
{
    if ( arnList[nCount] < nMin )
    {
        nMin = arnList[nCount];
	nIndex = nCount+1;
    }

    nCount++; // <- It should be here.
}


It should work now. If you need anymore help come back.

Reply With Quote
  #5  
Old December 12th, 2006, 07:34 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Thanks Paul, k with doing that, i get this as output.
Code:
The Minimum Number is -858993460 and the index is 75


75 being the total numbers in the file.

Thanks.

Reply With Quote
  #6  
Old December 12th, 2006, 08:02 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Ok, it's because your program is reading 100 elements into the array but you don't have 100 in your file. You are getting junk values. Replace this bit:

Code:
for ( int x=0; x < nSize; x++ )
{
    fin >> arnList[x];
}


With this:

Code:
int x = 0;

while( fin )  // While fin is good and not at EOF.
{
    fin >> arnList[x];
    ++x;
}


and change your function call to:

Code:
nMinFinder( arnList, x );


x now holds the true size of the array, or how many numbers you have counted.

I hope this helps.

Reply With Quote
  #7  
Old December 12th, 2006, 09:14 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Ignore that last post i did. It's 3 in the morning here, shouldn't really post when i'm tired. Don't know what i was thinking

It should be this:

Code:
int x = 0;
    int count = 0;

    while( fin )  // While fin is good and not at EOF.
    {
        fin >> x;
        arnList[count] = x;

        ++count;
    }

    nMinFinder( arnList, count );


Going to get some sleep now

Reply With Quote
  #8  
Old December 13th, 2006, 09:26 AM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Sorry Paul, to bother you this late,but what happens to nSize then.
Thanks once again,

Reply With Quote
  #9  
Old December 13th, 2006, 10:54 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
You don't need nSize in the loop. Your array can read up to 100 ints, but your file only has 75, so you just read in how many are in the file. When your program has finished reading the file, count will hold how many ints it has read. Your program will stop reading the file once all the ints have been read or it encounters EOF.

Reply With Quote
  #10  
Old December 13th, 2006, 11:01 AM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
k, Paul with this code, i get the same output
Code:
The Minimum Number is -858993460 and the index is 75


Code:
#include <iostream>
#include <fstream>
using namespace std;

void nMinFinder(int arnList[],int count);

int main()
{

	ifstream fin;
	int x = 0;
    int count = 0;
	int arnList[100];
	fin.open("numbers.txt");
    while( fin )  // While fin is good and not at EOF.
    {
        fin >> x;
        arnList[count] = x;
        ++count;
    }
    nMinFinder( arnList, count );
	return 0;
}

void nMinFinder (int arnList[],int count)
{
	int nIndex=0;
	int nMin=arnList[0];
	if (arnList[count]<nMin)
	{
	nMin=arnList[count];
	nIndex=count;
	}
	cout<<"The Minimum Number is "<<nMin<<" and the index is "<<nIndex<<endl;
}

Reply With Quote
  #11  
Old December 13th, 2006, 11:12 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Can you post your text file? It works for me but your text will be different.

Reply With Quote
  #12  
Old December 13th, 2006, 11:14 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Make sure your text file is in the same folder as your program.

Reply With Quote
  #13  
Old December 13th, 2006, 11:24 AM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Code:
#include <iostream>
#include <fstream>
using namespace std;

void nMinFinder( int arnList[],int count );

int main()
{
	ifstream fin;
	int x = 0;
    int count = 0;
	int arnList[100];
	fin.open("C:\\numbers.txt");

    while( fin )  // While fin is good and not at EOF.
    {
        fin >> x;
        arnList[count] = x;
        ++count;
    }

    nMinFinder( arnList, count );

	return 0;
}

void nMinFinder ( int arnList[], int count )
{
	int i = 0;
	int nIndex=0;
	int nMin=arnList[0];

	while ( i < count )
	{
		if ( arnList[i] < nMin )
		{
			nMin = arnList[i];
			nIndex = i + 1;
		}

		++i;
	}

	cout<<"The Minimum Number is " << nMin << " and the index is " << nIndex << endl;
}

Reply With Quote
  #14  
Old December 13th, 2006, 11:47 AM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Thank you so much Paul, for helping me out all the way, Sorry but there is one more thing i have to do for this program, i have to get a index number like say 10, and the value is (1), and then i get another index number like 22 (and say the value is 14), i have to switch the values of the index, like i would out put,

index 10=1
index 22=14,
swap index 10=14
swap index 22=1

, im working on the code rite now, i ll ask for help if i get stuck, and thanks once again.

Reply With Quote
  #15  
Old December 13th, 2006, 12:00 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Does it work ok?

Reply With Quote
  #16  
Old December 13th, 2006, 12:03 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Yea, it works Paul, I just edited my last post, i ll post the code in a while.
Thanks

Reply With Quote
  #17  
Old December 13th, 2006, 12:10 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
here is the code, i tried this earlier in class too, i didn't work, so plz Paul, help me out again,
Thanks

Code:
#include <iostream>
#include <fstream>
using namespace std;

void MinFinder (int arnList[],int count);
void Swap (int nRef1,int nRef2);

int main()
{

	ifstream fin;
	int x = 0;
    int count = 0;
	int arnList[100];
	fin.open("numbers.txt");
    while( fin )  // While fin is good and not at EOF.
    {
        fin >> x;
        arnList[count] = x;
        ++count;
    }
    nMinFinder( arnList, count );
	return 0;
}

void nMinFinder (int arnList[],int count)
{
	int i=0;
	int nIndex=0;
	int nMin=arnList[0];
	while(i<count)
	{
		if (arnList[i]<nMin)
		{
		nMin=arnList[i];
		nIndex=i+1;
		}
		i++;
	}
	cout<<"The Minimum Number is "<<nMin<<" and the index is "<<nIndex<<endl;
}

void Swap (int arnList[],int count,int& nRef1,int& nRef2)
{
	cout<<"Please give reference number 1"<<endl;
	cin>>nRef1;
	cout<<"Please give reference number 2"<<endl;
	cin>>nRef1;
	cout<<"index"<<nRef1<<" ="<<arnList[nRef1]<<endl;
	cout<<"index"<<nRef2<<" ="<<arnList[nRef2]<<endl;
	cout<<"swap index"<<nRef1<<" ="<<arnList[nRef2]<<endl;
	cout<<"swap index"<<nRef2<<" ="<<arnList[nRef1]<<endl;
}

Reply With Quote
  #18  
Old December 13th, 2006, 12:27 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Your function prototype is different from your function header:

Code:
void Swap (int nRef1,int nRef2); // Prototype


Code:
void Swap (int arnList[],int count,int& nRef1,int& nRef2) // Header


You function prototype must be the same as your function header.

You are not calling your function from anywhere. Inside main() have this function call ( that's if your are calling it from there )

Code:
swap( arnList, count );


Just like your last one. It looks to me like you just want to swap them in the swap function. Is that right? If you do, you don't need int &Ref1, int &Ref2. Just pass the address of the array and the size of the array like in that function call above. Answer these then get back to me to see if i am on the right track.

Reply With Quote
  #19  
Old December 13th, 2006, 01:02 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
Sorry about all the silly mistakes,

with this code these two errors.

Code:
G:\C++\Skill Set 10\Practice.cpp(22) : error C2065: 'nMinFinder' : undeclared identifier
G:\C++\Skill Set 10\Practice.cpp(28) : error C2373: 'nMinFinder' : redefinition; different type modifiers


Code:
#include <iostream>
#include <fstream>
using namespace std;

void MinFinder (int arnList[],int count);
void Swap (int arnList[],int count);

int main()
{

	ifstream fin;
	int x = 0;
    int count = 0;
	int arnList[100];
	fin.open("numbers.txt");
    while( fin )  // While fin is good and not at EOF.
    {
        fin >> x;
        arnList[count] = x;
        ++count;
    }
    nMinFinder( arnList, count );
    Swap (arnList,count);
	return 0;
}

void nMinFinder (int arnList[],int count)
{
	int i=0;
	int nIndex=0;
	int nMin=arnList[0];
	while(i<count)
	{
		if (arnList[i]<nMin)
		{
		nMin=arnList[i];
		nIndex=i+1;
		}
		i++;
	}
	cout<<"The Minimum Number is "<<nMin<<" and the index is "<<nIndex<<endl;
}

void Swap (int arnList[],int count)
{
	int nRef1,nRef2;
	cout<<"Please give reference number 1"<<endl;
	cin>>nRef1;
	cout<<"Please give reference number 2"<<endl;
	cin>>nRef1;
	cout<<"index"<<nRef1<<" ="<<arnList[nRef1]<<endl;
	cout<<"index"<<nRef2<<" ="<<arnList[nRef2]<<endl;
	cout<<"swap index"<<nRef1<<" ="<<arnList[nRef2]<<endl;
	cout<<"swap index"<<nRef2<<" ="<<arnList[nRef1]<<endl;
}

Reply With Quote
  #20  
Old December 13th, 2006, 01:11 PM
Paul820 Paul820 is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: United Kingdom
Posts: 428 Paul820 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 Days 7 h 55 m 4 sec
Reputation Power: 13
Hey, don't worry about little mistakes, it's all part of learning

Your problem is here in your function prototype:
Code:
void MinFinder (int arnList[],int count);


You are missing and 'n' infront of MinFinder. And another one, inside your swap function, you are asking for the value of nRef1 twice. Change one of them to nRef2.
Comments on this post
TheGuru agrees: Thanks Paul

Reply With Quote
  #21  
Old December 13th, 2006, 01:52 PM
TheGuru TheGuru is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2006
Posts: 39 TheGuru User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 17 m 44 sec
Reputation Power: 12
yep, finnaly done this, thanks for help once again Paul. appreciate it.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Function with array


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap