aomran 0 Newbie Poster

It was recommended by javaAdict, honestly I did not understand what it meant, but it did not work.
what i was trying to do is to read a line of integers separated by white space that is entered by the user and and store then into an array of integer as soon as the user hit enter.

aomran 0 Newbie Poster

thank a lot,
I traced the remove and the problem was simple, on line
heapArray[0] = heapArray[currentSize];
currentSize pointing to the next empty location of the array(past the end of the array), so I was getting null, so I just decrement it by one to point the the last node of the array
heapArray[0] = heapArray;
now I got a integer value and the program worked
Tracing by hand is really valuable tool

aomran 0 Newbie Poster

I am getting Exception in thread "main" java.lang.nullpointerException on line 113,92,200, need help

// heap.java 
// demonstrates heaps
// to run this program: C>java HeapApp
 import java.io.*;
 //
 //import java.util.Scanner;
 class Node 
	{ 
	private int iData;             // data item (key)
// ------------------------------------------------------------
	public Node(int key)           // constructor
	{ 
	iData = key; 
	System.out.println(" I am in costructor Node  " + iData);
	} 
// ------------------------------------
	public int getKey() 
	{ 
	//System.out.println(" I am in getkey");
	return iData; 
		
	}
// ------------------------------------------------------------
	public void setKey(int id) 
	{ 
	System.out.println(" I am in setKey");
	iData = id;
		

	}
// ------------------------------------------------------------
	} // end class Node
class Heap 
{ 
private Node[] heapArray; 
private int maxSize;           // size of array 
private int currentSize;       // number of nodes in array
// ------------------------------------------------------------
public Heap(int mx)            // constructor 
{ 
	maxSize = mx; 
	//currentSize = 0; 
	currentSize = 0; 
	heapArray = new Node[maxSize];  // create array
	} 
// ------------------------------------------------------------
	public boolean isEmpty() 			// if heap(array) is empty there os 0 element
	{ return currentSize==0; } 
// ------------------------------------------------------------
	public boolean insert(int key) 
	{ 
	if(currentSize==maxSize) 				// heap is full, can't insert
		return false; 
	Node newNode = new Node(key);
	System.out.println("newNode = " + newNode.getKey() + "CurrentSize = " + currentSize );  //+ " Node(key) = " + Node(key));               // Remove later	
	heapArray[currentSize] = newNode;
	//System.out.println(" heapArray[currentSize] = " + heapArray[currentSize] );					//Remove
	perculateUp(currentSize++); 						// point to next empty array location
	
		return true; 
	}  // end insert()
// ------------------------------------------------------------
	public void perculateUp(int index) 
	{ 
	System.out.println(" currentSize++ =  " + currentSize);
	int parent = (index-1) / …
aomran 0 Newbie Poster

thanks, javaAddict
Sorry for not replying fast enough because of time difference. I modified my program as you suggested, but the compiler did not except it, problem at line Scanner sc = new Scanner([B] Illegal start of expression, and other errors relating to the same line.
Can you please explain what does this line do

aomran 0 Newbie Poster

My input is 10 20 30 40 50 60
but when I entered it from the command prompt it does not print the content of the array , it seem that the while loop does not terminate and it is waiting for more input. the only way to kill it is to inter a character and not an integer. sc.hasNextInt() suppose to return a boolean true if the next token is an integer and false if anything else including eol. I have been working on this problem for three hours with no luck.

aomran 0 Newbie Poster

Sorry for posting it so fast.
I am trying to read a user input from the console as integer numbers and store them into an array. I used the scanner method hasNextInt() to check when no more integer there, and stop the while loop then print the array.
I am getting Exception in thread "main" java.lang.array

aomran 0 Newbie Poster
import java.util.*;
import java.util.Scanner;

public class InputconsoleArray2
    {
        public static void main(String args[]) throws java.io.IOException
        {
            int[] a=new int[100];								// create an array of type interger size
			// Objects of type Scanner are useful for breaking down formatted input into tokens and 
			// translating individual tokens according to their data type
			// By default, a scanner uses white space to separate tokens. (White space characters include blanks, tabs, 
			// and line terminators. 
            Scanner sc=new Scanner(System.in);
            System.out.println("Please enter elements...");
			int j=0;
			//int next= sc.nextInt();
			//System.out.println("Next = " + next);
			while (  sc.hasNextInt())
			{
				a[j]=sc.nextInt();
				System.out.println(" a[j]  " + j +"  " + a[j]);	
				j++;
			}
			sc.close();
			int arrayCurrentSize = j--;
         	System.out.println("Array elements are : ");
            for (int i=0; i< arrayCurrentSize; i++)
			  System.out.println(a[i]);
       }
   }
aomran 0 Newbie Poster

I have a new assignment about Huffman encoding, there is part of the assignment that is not clear to me, I searched the web, but could not find an answer, I need some help to understand this part. it is as follows:

The standard algorithm states that you should use a priority queue(aka heap) to store the trees. Use a heap of tree pointers instead, it makes combining the trees much easier and more efficient, and since they are pointers you don't have to copy any tree data. this technique is called indirect storage and is used on large and/or complex data type.

What is an indirect storage, and how can I use a heap of tree pointers.. Is there any link that explain indirect storage in details.

I highly appreciate your help, thanks

aomran 0 Newbie Poster

I have created a new project File--> New-->Project-->Consle Application, etc... a New project called assignment1 was created. then File-->New-->File-->C/C+ Source-->main--> saved under assignment1.
I did the same for Process. then I added two more header files C/C++ header, and saved them under assignment1. when it asked for file name I put main not main.cpp, same for header. assuming that codeblocks will add the extention for me. then I opened main and select build. this is when I got the message.
Thanks Dragon

aomran 0 Newbie Poster

I have two source files main.cpp and process.cpp and two header files dheap.h process.h all under project assignment1, I am using code blocks. I compliled main but I got the following message:
Linking stage skipped (build target has no object files to link)
Nothing to be done.

Is there anything that I missed, this is my first usage of code blocks.
thanks

aomran 0 Newbie Poster

Thank you

Ancient Dragon

it works, some one suggest to use Vector as another option, do you think it is a good idea?

aomran 0 Newbie Poster

when I tried to implement this

#include <sstream>  stringstream  processline;  
getline (myfile, processline);  
processline >> str_time;

I got a compilation error

c:\CBProjects\test\main.cpp|16|error: no matching function for call to `getline(std::istream&, std::stringstream&)'|
c:\CBProjects\test\main.cpp|17|error: no match for 'operator>>' in 'process >> processline'|

any suggestion why?

aomran 0 Newbie Poster

would the followind lines do the job

#include <sstream>
  stringstream  processline;
  getline (myfile, processline);
  processline >> str_time;
  processline >> priority  >> timerun  >> procname;

If my input
10 9 30 first process
.
.
and output the four variable I should get
str_time 10
priority 9
timerun 30
procname first process

aomran 0 Newbie Poster

I have put tags around my code as requested
this progran will simulate a process queue, giving time slices based on the priority. The process entries will be in a file, one per line.the data is the start time, priority level, total time to run and process name.
when I run the program I got few problem
1. how can I move to the next line of the file, it seems the program keeps reading from the same line.
2. some of the data on the file either give garbge or nothing such as procname which is processname.
I suspect there is a problem with my getline.

the following is a small portion of the total program but the problem is inside the while loop.

This is my input

10 9 15 program1
11 1 30 important
12 11 10 junk program
14 15 5 other program
17 6 20 another program

Please I need your help, I need to turn in my homework tomorrow.
thanks a lot.

#include <iostream>
#include <fstream>
#include<string>
#include <stdio.h>
#include <stdlib.h>
//using namespace std;
#include "process.h"
#include "dheap.h"
using namespace std;

int main(int argc, char *argv[])
{
    // argv is the array in which contains those arguments in a string format.
    if (argc != 4) // The number of arguments.
    {
        //TODO-reallocate
        cerr << "You Forget To Enter An Argument!"<<endl;
        exit(1);
    }

    char *TheFile = argv[1];  // is the …
aomran 0 Newbie Poster

by the way timing is initilized to zero, and timeu is the total time unit that the user provide, say 100. the while will end when we reach 99.

aomran 0 Newbie Poster

this progran will simulate a process queue, giving time slices based on the priority. The process entries will be in a file, one per line.the data is the start time, priority level, total time to run and process name.
when I run the program I got few problem
1. how can I move to the next line of the file, it seems the program keeps reading from the same line.
2. some of the data on the file either give garbge or nothing such as procname which is processname.
I suspect there is a problem with my getline.

the following is a small portion of the total program but the problem is inside the while loop.
Please I need your help, I need to turn in my homework tomorrow.
thanks a lot.

while (timing < timeU) // A loop for each time unit.

{
    if (myfile.is_open())
    {

        cout << "time =  " << timing << endl;
        getline(myfile, proname);
        myfile>> str_time;
        if (str_time != timing)
     //     myfile.putback(str_time);
        else //if (timing == str_time)// If the start time has arrived
        {
            myfile>> priority >> timerun; //data in the in the input.txt
            proc.set(priority, timerun, proname);    // To place in the process.
            dh.insert(proc);  // Insert the process.
            effpro = priority + timing; // To get the effective prority.
            effpro++; // to refelect the time.
            cout << " inserting  " << proname << endl;
        }

        //Read in and insert the next process.
   /*     if (timing == str_time)
        {
            nextOfproc.set(priority, …
aomran 0 Newbie Poster

thanks guys it works

aomran 0 Newbie Poster

I actually got these two messages

dheap.h: In member function ‘void Dheap<Process>::deleteMin() const’:
dheap.h:191: error: decrement of read-only location

aomran 0 Newbie Poster

I will post the relevant code shortly, but I need more explanation of what decrement of read only location. location of what and how it has been decremented.

aomran 0 Newbie Poster

Hi everyone,

Can anyone tells me what this error means:

error: decrement of read-only location.

thanks

aomran 0 Newbie Poster

just read few articles about template, but still could not figure out what those three error on the same line means. any hint?

dheap.h:3: error: ISO C++ forbids declaration of âDheapâ with no type
dheap.h:3: error: explicit instantiation of non-template âint Dheapâ
dheap.h:3: error: expected `;' before â<â token

Is my declaration of the template statement ok?

//#include <iostream>
#define MAX_SIZE 500 // Maximun size of the heap.
template Dheap <class Process>
{
public:
Dheap ( int _d = 2 ); //I have included the d value of the heap.
~Dheap (void);

aomran 0 Newbie Poster

Thanks for replying, I have tried to tag my code as recommended , I select all but could not find # on edit. is there other way to do it.

aomran

aomran 0 Newbie Poster

Thank you for taking the time to help me.
This is a portion of a D-Heap program to run a number of process based on priority level given to each process.
the following are some errors which I'm getting when trying to compile this code(this is a portion of the total code).
I think the errors are related to my definition of template.

dheap.h:3: error: ISO C++ forbids declaration of âDheapâ with no type
dheap.h:3: error: explicit instantiation of non-template âint Dheapâ
dheap.h:3: error: expected `;' before â<â token
dheap.h:35: error: expected constructor, destructor, or type conversion before â<â token
dheap.h:49: error: expected constructor, destructor, or type conversion before â<â token
dheap.h:59: error: expected initializer before â<â token
dheap.h:69: error: expected initializer before â<â token
dheap.h:80: error: expected initializer before â<â token
dheap.h:92: error: expected initializer before â<â token

-

//#include <iostream>
#define MAX_SIZE 500 // Maximun size of the heap.
template Dheap <class Process>
{
    public:
        Dheap ( int _d = 2 );        //I have included the d value of the heap.
        ~Dheap (void);
        void insert (const Process&); // To insert an element in the heap.
        void deleteMin (void);  // To remove an element in the heap and return it.
        bool IsEmpty(void)const;    // Return true if the heap is empty;otherwise false.
        void ToEmpty ();          // To make the heap empty.
        const Process&  min (void)const;   // To return the smallest key element in the heap.
        bool IsFull(void)const;    // To return true if the heap …