GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 08-Nov-2009, 23:43
linan2332's Avatar
linan2332 linan2332 is offline
New Member
 
Join Date: Aug 2009
Location: pleasant hill CA
Posts: 23
linan2332 is on a distinguished road

Array problem @@


Hi,
This is my 15th homework and I am still quite new to C++.
I have written this code but i cant find the mistake...
--------------------------------------------------------------------------
CPP / C++ / C Code:
/* Lin An ENGIN: 135 */
/* Homework 15 */
/* This program will arrangea set of number in ascending order and detect
   how many times another integers, entered by the user,appear in the set. */


#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;


//declare function sort
void sort (int x[ ] , int );


int main ()
{

	int A[100];
	int B[100];
	int i(0) ,number , n , j(0) ;


	//declare infile
	ifstream infile ("in.txt");

	do
	{
		infile>>A[i];
		B[i] = A[i];
		i++;
	}while(A[i] != -999);

	number = i ;

	//call function
	sort ( A , number );

	for ( i=0;i<=number;i++ )
	{
		B[i+50] = A[i];
	}

	//output
	cout<<setw(10)<<"Array Element"<<setw(10)<<"Orginal values"<<setw(10)<<"Array element"<<setw(10)<<"Sorted values"<<endl;
	cout<<setw(10)<<"----- -------"<<setw(10)<<"------- ------"<<setw(10)<<"----- -------"<<setw(10)<<"------ ------"<<endl;

	for ( i=0;i<=number;i++ )
	{
		cout<<setw(10)<<"A["<<i<<"]"<<setw[10]<<" "<<setw(10)<<"A["<<(i+50)<<"]"<<setw(10)<<" "<<endl;
	}

	cout<<"Enter a integer number: ";
	cin>>n;
	for (i=0;i<=number;i++)
	{
		if(A[i] == n)
		{
			j++;
		}
		else
		{
			;
		}
	}

	cout<<"Message : Number is <<"<<j<<"Not in the set/in the set/#"<<endl;


	//close file and end program
	infile.close();
	return 0;

}


//define function
void sort (int A[ ],int number )
{
	int i, j , temp ;
	for( i=0 ; i<number+1 ; i++ ) 
	{
		for( j=0 ; j< number-i ; j++ )
		{
			if(A[i] > A[i+1] )
			{
				temp = A[j+1];
				A[j+1] = A[j];
				A[j] = temp ;
			}
		}
	}
}

Here are the error
--------------------------------------------------------------------------
CPP / C++ / C Code:
1>------ Build started: Project: Homework 15, Configuration: Debug Win32 ------
1>Compiling...
1>HW115.cpp
1>c:\users\lin an\documents\c++\projects\project1\homework 15\homework 15\hw115.cpp(51) : error C2109: subscript requires array or pointer type
1>Build log was saved at "file://c:\Users\LIN AN\Documents\C++\Projects\Project1\Homework 15\Homework 15\Debug\BuildLog.htm"
1>Homework 15 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
__________________
just started to learn C++ :)
  #2  
Old 08-Nov-2009, 23:56
linan2332's Avatar
linan2332 linan2332 is offline
New Member
 
Join Date: Aug 2009
Location: pleasant hill CA
Posts: 23
linan2332 is on a distinguished road

Re: Array problem @@


Well i found my mistake at "setw[10]"
after i correct it...the prgoram still couldnt work even there is no error @@
__________________
just started to learn C++ :)
  #3  
Old 09-Nov-2009, 01:10
Kimmo Kimmo is offline
Regular Member
 
Join Date: Mar 2007
Location: Finland
Posts: 347
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: Array problem @@


Quote:
Originally Posted by linan2332
after i correct it...the prgoram still couldnt work even there is no error @@
Then it's a feature, not a bug. You might consider giving a little more descriptive error report. Meanwhile, start with this block.
CPP / C++ / C Code:
// Sometimes when you come into a familiar space, there is something...
// Wrong. Different from how it was the last you were here.
// But you just cannot seem to place what EXACTLY that is.
// Now think about this as one of those moments
do
	{
		infile>>A[i];
		B[i] = A[i];
		i++;
	}while(A[i] != -999);

EDIT

Okay, that was a lame joke. In your loop you increase the value of i by one each time. So what is the value of i when you finally get to the test in your loop? Is it as it should be, is it one less than it should be or is it one more than it should be?

Also, you test for the value -999. This is probably used to signal the end of the file? It seems like a poor practise. More recommended would be to use ifstream::eof(). Just remember it is true not until you have tried to actually read past the end.
Last edited by Kimmo : 09-Nov-2009 at 01:44.
  #4  
Old 09-Nov-2009, 01:35
linan2332's Avatar
linan2332 linan2332 is offline
New Member
 
Join Date: Aug 2009
Location: pleasant hill CA
Posts: 23
linan2332 is on a distinguished road

Re: Array problem @@


Well...after i try to execute the code..it just stop working and leave " Press anykey to continue..."
__________________
just started to learn C++ :)
  #5  
Old 09-Nov-2009, 01:51
ahbi82 ahbi82 is offline
Member
 
Join Date: Jul 2006
Posts: 202
ahbi82 will become famous soon enough

Re: Array problem @@


Quote:
Originally Posted by linan2332
Well...after i try to execute the code..it just stop working and leave " Press anykey to continue..."

What IDE are u using? Did you try step through using debug mode? If you aren't using IDE, try printing out those values to screen to see any if those values make any sense. From your code, i think it should at least print something. Did some dialog box pop up and say an error occur?

ITS BEST YOU TRY STEPPING THROUGH IN DEBUG MODE.

CPP / C++ / C Code:
	int A[100];
	int B[100];
	int i(0) ,number , n , j(0) ;


	//declare infile
	ifstream infile ("in.txt");

	do
	{
		infile>>A[i];
		B[i] = A[i];
		i++;
	}while(A[i] != -999);   // BIG PROBLEM
The above code will cause stack overflow. Rephrase the loop again. Whats ur terminating condition? Whats the format of your file? How are the data arranged in the file??
Attach to this post, i'll try to see "if i have time"... hehe. But this loop is a big problem.

Hope this helps!!!
Jeremy
  #6  
Old 09-Nov-2009, 06:05
zalezog zalezog is offline
Junior Member
 
Join Date: Oct 2007
Posts: 45
zalezog will become famous soon enough

Re: Array problem @@


First arrays are zero indexed.
Quote:
Originally Posted by linan2332
CPP / C++ / C Code:
for ( i=0;i<=number;i++ )

for (i=0;i<=number;i++)

for( i=0 ; i<number+1 ; i++ ) 
	{
		for( j=0 ; j< number-i ; j++ )

In all above for loops, you are trying to access the n'th element of the array.
if you have n elemnts then array access is:
CPP / C++ / C Code:
for (int i = 0; i < n; i++)
    A[i] = ...
Quote:
CPP / C++ / C Code:
ifstream infile ("in.txt");
//What if the file does not exist ? or, there were errors in opening a file ?
	do
	{
		infile>>A[i];
		B[i] = A[i];
		i++;
	}while(A[i] != -999);

	number = i ;
Apart from Kimmo pointing you out the post increment i problem
Quote:
Originally Posted by Kimmo
In your loop you increase the value of i by one each time. So what is the value of i when you finally get to the test in your loop? Is it as it should be, is it one less than it should be or is it one more than it should be?


Although you are using bubble sort and comparing consecutive elements, you are still comparing i and (i + 1)th elements repeatedly
Quote:
Originally Posted by linan2332
CPP / C++ / C Code:
if(A[i] > A[i+1] )                        //Should have been j

I did the file reading part this way:
CPP / C++ / C Code:
#include<iostream>
#include<fstream>
/*If your homework really requires a sentinel*/
const char *filename = "in.txt";
enum Constants {
     SENTINEL = -999,
     MAX_ELEMENTS = 100,
	 
};
int main() {
    
	int elements[MAX_ELEMENTS] = { 0 };
	std::ifstream infile(filename);
	int number_of_elements = 0;
	if (infile.is_open()) { //Whether a valid file or not
	    int temp = 0;
		
		while (infile >> temp) {
			if (temp != SENTINEL && number_of_elements < MAX_ELEMENTS) {
			    elements[number_of_elements++] = temp;
			}
		}
		std::cout << "Number of elements read : " << number_of_elements; 
	    //Now the sorting part..
	}
	else {
		std::cout << "File not found";
	}
	return 0;
}
 
 

Recent GIDBlogNot selected for officer school by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to array of pointers to objects problem icedown C++ Forum 1 06-Oct-2009 22:35
where is the problem and can you fix it (php) oggie MySQL / PHP Forum 8 14-Apr-2008 15:08
Getting a line error in register oggie MySQL / PHP Forum 5 13-Apr-2008 16:16
Returning a 2 dimensional Array from a function vicky_brsh C++ Forum 1 04-Jan-2008 14:06
Need help deleting the last element in the array headphone69 C++ Forum 2 15-Mar-2006 19:31

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:40.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.