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 24-Jan-2007, 19:24
krazykrisi krazykrisi is offline
New Member
 
Join Date: Sep 2006
Posts: 20
krazykrisi is an unknown quantity at this point

sorting


I don't know why this program isn't compiling right. Maybe I'm not getting something right? Some help would be greatly appreciated.
CPP / C++ / C Code:
/*Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint()
that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array 
and the number of elements in it, and return the index number of the largest element. The program should call this function and then display 
the largest element and its index number.*/

//sales.cpp
//averages a week's widget sales (6 days)
#include <iostream>
using namespace std;

int main()
{
    const int SIZE = 6;        //size of array
    int integers[SIZE];        //array of 6 variables

    cout << "Enter 6 integers:\n";
    int in;
    cin >> in;
    maxint(integers[SIZE], in);
    int xy;
    cin >> xy;
    return 0;
}
void maxint(int x, int y)
{
    for(int j=0; j<x; j++)
        cin >> integers[j];
    int total = 0;
    for(int j=0; j<x; j++)
        total += integers[j];
    int average = total / x;
    cout << "Average = " << average << endl;
}
Last edited by admin II : 26-Jan-2007 at 08:46. Reason: please surround your c++ code with [cpp] ... [/cpp]
  #2  
Old 24-Jan-2007, 20:35
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: sorting


Quote:
Originally Posted by krazykrisi
I don't know why this program isn't compiling right. Maybe I'm not getting something right? Some help would be greatly appreciated.
prototype

CPP / C++ / C Code:
#include <iostream>
using namespace std;

void maxint(int x, int y);

int main()
{
  #3  
Old 24-Jan-2007, 21:00
BlueFireCO. BlueFireCO. is offline
Junior Member
 
Join Date: Dec 2006
Posts: 41
BlueFireCO. is an unknown quantity at this point

Re: sorting


Hey, ok for starters all the definitions for the variables should be at the top. Secondly, your directions are confusing, one says you should average numbers, the other says you should find the largest number in the array. from what i understand, this is how i would write your main function...
(I would have them enter the numbers before it gets to the function)
CPP / C++ / C Code:
int main()
{
    const int SIZE = 6;        //size of array
    int integers[SIZE];        //array of 6 variables
    int xy;
        int counter;

    cout << "Enter 6 integers:\n";

        while ( counter != 6){
            counter ++;
            cout << " Enter week " << counter << " : ";
        } 

    maxint(integers[SIZE]); // What was " in " used for??

    cin >> xy;
    return 0;
}

and now for the function, ( which from my understanding should find the largest number in the array and give it's index number.)
CPP / C++ / C Code:
void maxint( array[]){
        int position1 = 1;
        int increment = 1;
        int looper = 0;
        int IndexNumber = 1;

  while( looper != 6){
        if ( array[position1] <= array[position1 + increment]){
            position1 ++;
            increment = 1;
            looper = 0;
        }
        else{
            increment ++;
            looper ++;
        }
  }

        cout << " The largest number is stored in array["<<position1<<"].\n"
        cout << " The number in that area is " << array[position1] <<"\n\n";
}
            

that should work, if it doesnt, notify me.

-Matt
Last edited by admin II : 26-Jan-2007 at 08:47. Reason: please surround your c++ code with [cpp] ... [/cpp]
  #4  
Old 24-Jan-2007, 21:13
krazykrisi krazykrisi is offline
New Member
 
Join Date: Sep 2006
Posts: 20
krazykrisi is an unknown quantity at this point

Re: sorting


I'm still getting errors. Here is the code:
CPP / C++ / C Code:
/*Start with a program that allows the user to input a number of integers, and then stores them in an int array. Write a function called maxint()
that goes through the array, element by element, looking for the largest one. The function should take as arguments the address of the array 
and the number of elements in it, and return the index number of the largest element. The program should call this function and then display 
the largest element and its index number.*/

//sales.cpp
//averages a week's widget sales (6 days)
#include <iostream>
using namespace std;
void maxint(int array[]);
int main()
{
    const int SIZE = 6;        //size of array
    int integers[SIZE];        //array of 6 variables
    int xy;
        int counter;

    cout << "Enter 6 integers:\n";

        while ( counter != 6){
            counter ++;
            cout << " Enter week " << counter << " : ";
        } 

    maxint(integers[SIZE]);

    cin >> xy;
    return 0;
}
void maxint(int array[])
{
        int position1 = 1;
        int increment = 1;
        int looper = 0;
        int IndexNumber = 1;

  while( looper != 6){
        if ( array[position1] <= array[position1 + increment]){
            position1 ++;
            increment = 1;
            looper = 0;
        }
        else{
            increment ++;
            looper ++;
        }
  }

        cout << " The largest number is stored in array["<<position1<<"].\n"
        cout << " The number in that area is " << array[position1] <<"\n\n";
}

The errors I am getting are line 25, which is:
Code:
maxint(integers[SIZE]);
invalid conversion from 'int' to 'int'

What's the problem?
Last edited by admin II : 26-Jan-2007 at 08:48. Reason: please surround your c++ code with [cpp] ... [/cpp]
  #5  
Old 24-Jan-2007, 21:32
BlueFireCO. BlueFireCO. is offline
Junior Member
 
Join Date: Dec 2006
Posts: 41
BlueFireCO. is an unknown quantity at this point

Re: sorting


give me some time to figure it our, i rid of the errors, but it needs some fixing.
  #6  
Old 24-Jan-2007, 21:41
krazykrisi krazykrisi is offline
New Member
 
Join Date: Sep 2006
Posts: 20
krazykrisi is an unknown quantity at this point

Re: sorting


Ok. Thank you. In the mean time, I am working on another program and getting errors with it as well. I think my problem is with the directions, they're so vague. Anyway, here is the code if you can figure this out too, or someone else please?

CPP / C++ / C Code:
/*A queue is a data storage device much like a stack. The difference is that in a stack the last data item stored is 
the first one retrieved, while in a queue the first data item stored is the first one retrieved. That is, a stack 
uses a last-in-first-out (LIFO) approach, while a queue uses first-in-first-out(FIFO). A queue is like a line of 
customers in a bank: The first one to join the queue is the first one served. 
Rewrite the stakaray program to incorporate a class called queue instead of a class called stack. Besides a 
constructor it should have two functions: one called put() to put a data item on the queue and one called get() 
to get data from the queue. These are equivalent to push() and pop() in the stack class.
Both a queue and a stack use an array to hold the data However, instead of a single int variable called top, as the 
stack has, you'll need two variables for queue: one called head to point to the head of the queue, and one called 
tail to point to the tail. Items are placed on the queue at the tail (like the last customer getting in line at the 
bank) and removed from queue. This results in an added complexity: When either the tail or the head gets to the end 
of the array, it must wrap around to the beginning. Thus you'll need a statement like
if(tail == MAX-1)
    tail = -1;
to wrap the tail, and a similar one for the head. The array used in the queue is sometimes callede a circular buffer,
because the head and the tail circle around it, with the data between them.*/
#include <iostream>
using namespace std;
//////////////////////////////////////////////////////////
class Queue
{
private:
    enum { MAX = 10 };    //(non-standard syntax)
    int qu[MAX];            //queue:array of integers
    int head, tail;                //number of top of stack
public:
    Queue()            //constructor
    { tail = -1; head = +1 }
    void put(int var)        //put number on queue
    { qu[++top] = var; }
    int get()                //take number off queue
    { return qu[top--]; 
      if(tail == MAX)
              tail = -1;
     }
};
//////////////////////////////////////////////////////////
int main()
{
    Queue queue1;
    int xy;
    cout << queue1;
    
    cin >> xy;
    return 0;
}
Last edited by admin II : 26-Jan-2007 at 08:49. Reason: please surround your c++ code with [cpp] ... [/cpp]
  #7  
Old 25-Jan-2007, 05:38
Cristovao Cristovao is offline
New Member
 
Join Date: Jan 2007
Posts: 6
Cristovao is on a distinguished road

Re: sorting


First, why do you use an enumeration when all you define is a single variable which is supposed to be a constant (The maximum number of elements)? I suggest that you place a line after the include of this sort:
#define MAX 10
or in case you dont want this "macro" through all your program, make a constant in the classe's private area. (const int MAX 10)

Next thing, you forgot a semi-colon after head = +1; and I also suggest you initialise it to 0 instead of 1. Obviously by changing this the logic of put() and get() will be slightly altered.

Your next problem is that there is no variable top, which you use as an index of the array. Then in function get() you do operations after the return, however as you have returned, they will not happen!

And finally cout << queue1; is certainly giving you an error as this operation is not defined for classes. So this is my sugestion as the full program corrected, and asking some questions to the user (if he wishes to add a new element or retrieve one)

CPP / C++ / C Code:
/*A queue is a data storage device much like a stack. The difference is that in a stack the last data item stored is 
the first one retrieved, while in a queue the first data item stored is the first one retrieved. That is, a stack 
uses a last-in-first-out (LIFO) approach, while a queue uses first-in-first-out(FIFO). A queue is like a line of 
customers in a bank: The first one to join the queue is the first one served. 
Rewrite the stakaray program to incorporate a class called queue instead of a class called stack. Besides a 
constructor it should have two functions: one called put() to put a data item on the queue and one called get() 
to get data from the queue. These are equivalent to push() and pop() in the stack class.
Both a queue and a stack use an array to hold the data However, instead of a single int variable called top, as the 
stack has, you'll need two variables for queue: one called head to point to the head of the queue, and one called 
tail to point to the tail. Items are placed on the queue at the tail (like the last customer getting in line at the 
bank) and removed from queue. This results in an added complexity: When either the tail or the head gets to the end 
of the array, it must wrap around to the beginning. Thus you'll need a statement like
if(tail == MAX-1)
	tail = -1;
to wrap the tail, and a similar one for the head. The array used in the queue is sometimes callede a circular buffer,
because the head and the tail circle around it, with the data between them.*/
#include <iostream>
using namespace std;
#define MAX 10
//////////////////////////////////////////////////////////
class Queue
{
private:
	int qu[MAX];				//queue:array of integers
	int head, tail;				//head, has the first ocupied position
								//tail holds the last, when they are the same there is only one element
	int elements;				//This variable is needed as the situation in which there are no elements and the one
								//when the queue is full seem the same, if analyzing only the "pointers"
public:
	Queue()			//constructor
	{
		tail = -1; 
		head = 0;
		elements = 0;
	}
	void put(int var)		//put number on queue
	{
		if(elements == MAX)
			cout << "The queue is full!\nYou can't place any more elements.\n";
		else
		{
			tail = (tail+1)%MAX;//update "pointer" to the tail
								//The % operator is the rest of the division by MAX
								//Like so, when tail + 1 == Max it will give tail the value 0
			qu[tail] = var;		//place the number on the queue
			elements++;
		}
		
		return;
	}
	int get()				//take number off queue
	{
		if(elements == 0)
		{
			cout << "There are no elements in the queue!\n";
			return 0;
		}
		else
		{
			elements--;					//Decrease the number of elements
			head = (head+1)%MAX;		//Update "pointer" to head
			return qu[((head-1)==0)?(MAX-1):(head-1)];
										//return the value, in case head "points" to the beginning, the element we want would be at the end
										//Therefore the " ? : " operator, which works like if
		}
	}
};
//////////////////////////////////////////////////////////
int main()
{
	Queue queue;
	char ans;
	int xy;
    
	do{
		cout << "\tWhat do you want to do?\n(a - add value; r - retrieve value; e- exit)\n\t\t->";
		cin >> ans;

		switch(ans)
		{
		case 'a':
		case 'A':
			cout << "Which value do you want to add?\n\t\t->";
			cin >> xy;
			queue.put(xy);
			break;
		case 'r':
		case 'R':
			xy = queue.get();
			if(xy != 0)
				cout << "The value is:\t" << xy << "\n";
			break;
		}
	}while(ans != 'e' && ans != 'E');
    
    return 0;
}

I believe this is it and I hope this helps...
  #8  
Old 25-Jan-2007, 08:12
krazykrisi krazykrisi is offline
New Member
 
Join Date: Sep 2006
Posts: 20
krazykrisi is an unknown quantity at this point

Re: sorting


Thank you, that seems to work just fine. I have another problem, this is the last one I swear! I'm totally lost with this program. Any help would be appreciated!
CPP / C++ / C Code:
/*A matrix is a two-dimesional array. Create a class matrix that provides the same safety feature as the array class 
in Exercise 7; that is, it checks to be sure no array index is out of bounds. Make the member data in the matrix 
class a 10-by-10 array. A constructor should allow the programmer to specify the actual dimensions of the matrix 
(provides they're less than 10 by 10). The member functions that access data in the matrix will now need two index 
numbers: one for each dimension of the array.Here's what a fragment of a main() program that operates on such a 
class might look like:
matrix m1(3, 4);    //define a matrix object
int temp = 12345;    //define an int value
m1.putel(7, 4, temp);    //insert value of temp into matrix at 7 ,4
temp = m1.getel(7, 4);    //obtain value from matrix at 7 ,4*/
#include <iostream>
using namespace std;
class Matrix
{
      private:
               int rows, columns;
               int matrix[][];
              
              public:
                     Matrix()
                     { rows=10; columns=10; matrix[rows][columns]; }
                     void putel(rows, columns)
                     {
                      }
                      int getel(rows, columns)
                      {
                      }
};
/////////////////////////////////////////////////////////////
int main()
{
    Matrix m1(2,4);                    //object of class matrix
    int temp = 12345;                  //defines an int value
    int xy;    //dummy variable
    m1.putel(7, 4, temp);              //insert value of temp into matrix at 7, 4
    temp = m1.getel(7, 4);             //obtain value from matrix at 7, 4
    
    cin >> xy;
    return 0;
}
Last edited by admin II : 26-Jan-2007 at 08:49. Reason: please surround your c++ code with [cpp] ... [/cpp]
  #9  
Old 25-Jan-2007, 14:41
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: sorting


Quote:
Originally Posted by krazykrisi
The errors I am getting are line 25, which is:
Code:
maxint(integers[SIZE]);
invalid conversion from 'int' to 'int'

What's the problem?
You are passing in a single int when the function is expecting an array. That is not even a valid index in the array.

Quote:
Originally Posted by krazykrisi
Thank you, that seems to work just fine. I have another problem, this is the last one I swear! I'm totally lost with this program. Any help would be appreciated!
What is your question?
  #10  
Old 26-Jan-2007, 00:13
krazykrisi krazykrisi is offline
New Member
 
Join Date: Sep 2006
Posts: 20
krazykrisi is an unknown quantity at this point

Re: sorting


Here is what I have now:
CPP / C++ / C Code:
#include <iostream>
#include <conio.h> // for getch()
using namespace std;
class Matrix
{
      private:
            int MAX; //initialized to 10 in the initializer list
            static int width;
            static int height;
            int array[width][height];
            int real_width; //width and height chosen by the user
            int real_height;
      public:
             Matrix(int numRows=10, int numColumns=10)
             { rows=numRows; columns=numColumns;
              matrix = new int[][rows];
              for(int i = 0; i < rows; i++)
                 matrix[i] = new int [columns];  
             }
};
/////////////////////////////////////////////////////////////
Matrix::Matrix(int width, int height) : MAX(10)              //constructor
{
    if(real_width || real_height > MAX)  //if width or height is larger than MAX
    {
                  //do something, e.g initialize real_width and real_height to zero 
                 real_width=0;
                 real_height=0;
                  //to mark an invalid Matrix
                  else     //else initialize real_width and real_height with width and height
                  real_width=height;
                  real_width=width;
    }
}
////////////////////////////////////////////////////////////////
}
int main()
{
    Matrix m1(2,4);                    //object of class matrix
    int temp = 12345;                  //defines an int value
    m1.putel(7, 4, temp);              //insert value of temp into matrix at 7, 4
    temp = m1.getel(7, 4);             //obtain value from matrix at 7, 4
   
    
    cout << "\nPress any key ... \n" << flush;
    getch(); // for pausing

    return 0;
}
why am I getting errors and how should I fix this?
Last edited by admin II : 26-Jan-2007 at 08:50. Reason: please surround your c++ code with [cpp] ... [/cpp]
 
 

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
help with sorting d-dimensional points acosgaya C++ Forum 2 25-Jun-2006 08:35
question on sorting items is not clear...plz help me gvsivannarayana C Programming Language 0 28-Mar-2006 00:26
Problem with sorting algorithm MM4o C++ Forum 3 24-Feb-2006 09:40
sorting string w/strcmp doc_watson2007 C Programming Language 4 20-Nov-2004 09:17
sorting question fj8283888 C Programming Language 1 13-Apr-2004 20:42

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

All times are GMT -6. The time now is 06:08.


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