waldis 0 Newbie Poster

This is what I use, it pauses right before exiting, it's pretty much the same as getchar(), only difference that pressing any key works to exit, where if you use getchar() you have to hit return (enter) to exit

system("PAUSE");
return EXIT_SUCCESS;

Cheers,

Waldis

waldis 0 Newbie Poster

It's brilliant, thank you for saving my head from hitting it against the wall, so to speak.

Thank you,

Waldis

waldis 0 Newbie Poster

Attachment? What attachment?

Good point... let me try once more to attach it, I must've done something wrong.

waldis 0 Newbie Poster

Hi,

Once I had a similar problem when I tried to read a binary file on a pc and I failed, but I was able to read it in Linux (there were actually different versions of the file), so I figured it's a corrupted file, thou the hex dump looked ok.

Now I'm having this problem with a different file, and on both, PC and Linux. The file structure is simple, int (4 bytes) and 15 ascii characters (15 bytes)

When I read it in I get the 1st and the 20th records right, all the rest of them show up wrong.

I'm attaching the binary file with extension .txt since .dat extension seems not to be a valid file extension for attachment on the forum. and I changed the extension in the code as well to reflect this change.

/* read.c */

#include <stdio.h>
#include <stdlib.h>

#define NAMELEN 15

typedef struct
{
    int num;
    //char junk[4];
    char name[NAMELEN];
} rectype;

int main(int argc, char *argv[])
{
    rectype rec;
    FILE *f;

    /* initialize */
    f = fopen("extsort.txt","rb"); /* originally extsort.dat */
    
    /* Temp */
    int i;
    for(i=1; i<35; ++i)
    {
        fread(&rec,sizeof(rectype),1,f);
	    printf("Record #%3d is %15d --> %-15s\n",i,rec.num,rec.name);
    }

    /* process each record in file */
    //while (fread(&rec,sizeof(rectype),1,f) == 1)
    //{
	//printf(" %15d --> %-15s\n",rec.id,rec.name);
    //}

    /* finalize */
    fclose(f);

	system("PAUSE");
	return EXIT_SUCCESS;
}  /* main */

/* end read.c */

Thank you,

Waldis

waldis 0 Newbie Poster

Thank you very much for your help!

Waldis

waldis 0 Newbie Poster

I appreciate your input! I really feel that I should've started all this programming adventure some 25 years ago when I was in my teenage years, but time has gone by and I have to settle with what I got...

I have added a counting semaphore class (I have to implement it myself without using Java built in semaphores), and changed some things around. When I run it, it displays numbers, and the result is correct, but it takes considerable time (half a minute or so) to count. I guess I have done something wrong again...

class CountingSemaphore {
    protected int count;

    public CountingSemaphore() {
        count=0;
    }

    public synchronized void take() throws InterruptedException {
        while (count==0)
            wait();
        count--;
    }

    public synchronized void add() {
        count++;
        notify();
    }

    public synchronized int get() {
        return count;
    }
}

class TestThread extends Thread {
    public void run() {
        for (int i = 1; i <= SampleRace.N1; i++) {
            SampleRace.calcsum();
        }
    }
}

public class SampleRace {
    public static final int N1 = 1000;
    public static final int N2 = 10000;
    public static CountingSemaphore sum = new CountingSemaphore();

    public static void calcsum() {
        for (int i = 0; i < N2; i++)
            sum.add();
        System.out.println(sum.get());
    }

    public static void main(String[] args) throws InterruptedException {
        TestThread t1 = new TestThread();
        t1.start();

        TestThread t2 = new TestThread();
        t2.start();

        t1.join();
        t2.join();

        System.out.println("Expected sum: " + 2 * N1 * N2);
        System.out.println("Actual sum:   " + sum.get());
    }
}

Thanks again,

Waldis

waldis 0 Newbie Poster

Hi, I'm trying to get the threads synchronized, but the output I get is still wrong:

Expected sum: 20000000
Actual sum: 10000000

Do I use synchronized wrong, or do I put it in a wrong place?

class TestThread extends Thread
   {
   public void run()
      {
      for (int i = 1; i <= SampleRace.N1; i++)
         {
         //System.out.println(SampleRace.sum);
         SampleRace.sum = SampleRace.calcsum(SampleRace.sum);
         }
      } // run
   } // class TestThread



public class SampleRace
   {
   public static final int N1 = 1000;
   public static final int N2 = 10000;
   public static int sum = 0;

   public synchronized static int calcsum(int sum)
      {
      int tmp;

      tmp = sum;
      for (int i = 0; i < N2; i++)
          tmp = tmp + 1;
      //System.out.println(sum);
      return(tmp);
      }

   public static void main(String[] args) throws InterruptedException
      {

      // start the tasks
      TestThread t1 = new TestThread();
      t1.start();

      TestThread t2 = new TestThread();
      t2.start();

      // wait for the tasks to finish
      t1.join();
      t2.join();

      // print results
      System.out.println("Expected sum: " + 2 * N1 * N2);
      System.out.println("Actual sum:   " + sum);
      } // main
   } // class SampleRace

Thanks,

Waldis

waldis 0 Newbie Poster

Here it goes, I got rid of eof() and changed the while loop around...

void getnumber(int *num, bool *endfile)
{
     int tmpnum = 0;

// if ncount is larger, buffer is empty
// and it's time to read in first/next 10 records
if(ncount > pcount)
{
     //reset counters (in case it's a re-run ;)
     pcount = -1;
     ncount = 0;

     while((f.good()) && (pcount < MAXLEN-1))
     {
         //get record into buffer
         f >> tmpnum >> ws; //without ws if there is a new line at the end
                                        //it outputs last number twice
         buffer[++pcount] = tmpnum;
     }
}
     //assign endfile status
     *endfile = (ncount > pcount);
     
     //if not endfile, pass next record from buffer
     if(!*endfile)
         *num = buffer[ncount++];
}

Thanks,

Waldis :)

waldis 0 Newbie Poster

Here is what I came up with. The infinite loop the program was giving was a side effect of not being able to read the .dat file for some reason (path was too long or something), so when I moved it to c:\tmp\ it actually didn't do it no more.

I modified the code as you suggested, left -1 for pcount though so I could compare it with ncount, commented as much as I could, and it run "almost" right... it's loosing every eleventh integer out of .dat file.

I added some cout's to see where I'm loosing it, so it happens at the f >> tmpnum; within the loop after pcount reaches 9 and reads an extra integer, and then it looses it.

Here's the code:

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

void startnumber();
void finish();
void getnumber(int *num, bool *endfile);
void printnumber(int num);

const int MAXLEN = 10;

int buffer[MAXLEN];
int ncount;
int pcount;
ifstream f;

void startnumber()
{
     pcount = -1;
     ncount = 0;
     
     f.open("P2355.DAT",ios::in);
     assert(!f.fail());
}

void finish()
{
     f.close();
     //assert(!f.fail());
}

void getnumber(int *num, bool *endfile)
{
     int tmpnum;

// if ncount is larger, buffer has been emptied
// and time to read next 10 records
if(ncount > pcount)
{
     //reset counters
     pcount = -1;
     ncount = 0;

     //read the first record, so we can check the eof()
     f >> tmpnum;
     cout << "first num: " << tmpnum << endl;
     while((!f.eof()) && (pcount < MAXLEN-1))
     {
         //get records into buffer …
waldis 0 Newbie Poster

Thanks for reply!

Here is what the program should do:

------------------------------------------
=======================
------------------------------------------

Your task is to develop a procedure "getnumber(value,eof)" which
will return the value of the next number stored in a disk file and set eof
(end of file) to false, or set eof to true if there is no more data in the file.
The getnumber procedure will interface with the disk file through a 10 element
buffer (i.e. at most 10 numbers from the file are resident in memory at one
time). The input file contains several lines of data, with a single integer
value on each line.

In addition to the getnumber procedure, you will need to write a procedure
"startnumber" which sets whatever is necessary to cause getnumber to return
values beginning at the first number in the file. Furthermore, you will need to
write a main driver program which tests the getnumber procedure by using it to
write out all numbers that are in the file. The pseudocode for the main program
is shown below:

startnumber
    getnumber(num,endfile)
    while not endfile
         print(num)
         getnumber(num,endfile)

------------------------------------------
=======================
------------------------------------------

As fas as what it is doing... it outputs one and the same address in infinite loop :sad:

Thanks.

waldis 0 Newbie Poster

I re-wrote the code, and still having trouble :sad:

Here's code:

#include <fstream>
#include <iostream>
//#include <strstream>
using namespace std;

const int MAXLEN = 10;
int buffer[MAXLEN];
int pcount;
int ncount;
ifstream f;


void startnumber()
{
     pcount = -1;
     ncount = 0;
     
     f.open("P2355.DAT",ios::in);
     //assert(!f.fail());
}

void finish()
{
     f.close();
     //assert(!f.fail());
}

void getnumber(int *num, bool *endfile)
{
     int tmpnum;
     
     f >> tmpnum;
     
     while((!f.eof()) && (pcount < MAXLEN-1))
     {
         buffer[++pcount] = tmpnum;
         f >> tmpnum;
     }
     
     *endfile = ((bool)f.eof());
     if(!endfile)
         *num = buffer[ncount++];
}

void printnumber(int *num)
{
     cout << num << endl;
}



int main(void)
{
    int num;
    bool endfile;
    
    startnumber();
    getnumber(&num, &endfile);
    
    while(!endfile)
    {
         printnumber(&num);
         getnumber(&num, &endfile);
    }
    return 0; 
}

Any help would be appreciated.

Waldis

waldis 0 Newbie Poster

I'm having difficulties to get the buffer read 10 numbers from a file, then after it's empty read next 10 numbers, and so on till eof is reached. The input file contains several lines of data, with a single integer value on each line. Here is my code (I have commented where help is needed):

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

const int MAXLEN = 10;

class BufferClass
{
  private:
          int buffer[MAXLEN];
          ifstream f;
  
  public:
         BufferClass();
         ~BufferClass();
         void startNumber();
         void getNumber(int &num, bool &endfile);
         void printNumber(int &num);
};

BufferClass::BufferClass()
{
     //no use, since startNumber() takes care of opening the file?!?
}

BufferClass::~BufferClass()
{
     f.close();
}

//sets whatever is necessary to cause getNumber to return
// values beginning at the first number in the file
void BufferClass::startNumber()
{
     f.open("p2355.dat");
}

//getNumber() returns the value of the next number stored in a disk file
//and set endfile to false, or set endfile to true if there
//is no more data in the file
//getNumber() has to interface with the disk file through
//a 10 element buffer (i.e. at most 10 numbers from
//the fileare resident in memory at one time
void BufferClass::getNumber(int &num, bool &endfile)
{
     //read ten numbers from file
     //
}

void BufferClass::printNumber(int &num)
{
     cout << num << endl;
}



int main(void)
{
    BufferClass buffer;
    int num;
    bool endfile;
    
    buffer.startNumber();
    buffer.getNumber(num, endfile);
    
    while(!endfile)
    {
         buffer.printNumber(num);
         buffer.getNumber(num, endfile);
    }
    return 0; 
}

Thank you,

Waldis

waldis 0 Newbie Poster

Thank you, Narue. That website is really helpful for more than just a heap sort.

waldis 0 Newbie Poster

We were given a heap sort in a pseudocode:

******* HEAP SORT ******* 

sift(A, l, r)
-------------
f <- l
s <- 2 * l
temp <- A[f]
found <- false
while not found and s <= r
    if s < r and A[s-1] < A[s] then
        s <- s+1
    if temp >= A[s-1] then
        found <- true
    else
        A[f-1] <- A[s-1]
        f <- s
        s <- 2 * f
A[f-1] <- temp


heapsort(A,N)
-------------
l <- N / 2 + 1
r <- N
while l > 1
    l <- l - 1
    sift(A, l, r)
while r > 1
    temp <- A[0]
    A[0] <- A[r-1]
    A[r-1] <- temp
    r <- r - 1

:~~~~~ Average case performance: O(23N Ln N)
:~~~~~ Worst case performance:   O(26N Ln N)

A => array
N => size of array
l => left edge
r => right edge
f => father pointer
s => son pointer

I put it in a java code:

//Heap Sort: sift() and heapsort()
    private static void sift(int[] a, int leftEdge, int rightEdge)
    {
        int fatherPointer = leftEdge;
        int sonPointer = 2 * leftEdge;
        int temp = a[fatherPointer];
        boolean found = false;
        
        while(!found && sonPointer <= rightEdge)
        {
            if(sonPointer < rightEdge && a[sonPointer-1] < a[sonPointer])
                sonPointer = sonPointer + 1;
            if(temp >= a[sonPointer-1])
                found = true;
            else
                a[fatherPointer-1] = a[sonPointer-1];
            fatherPointer = sonPointer;
            sonPointer = 2 * fatherPointer;
        }//while(!found && sonPointer <= rightEdge)
        
        a[fatherPointer-1] = temp;
    }//private static void sift(int[] a, int leftEdge, int rightEdge)

    public static …
waldis 0 Newbie Poster

Thank you. If I'm correct there should be an else clause before C. Let me rewrite the pseudocoude the way I'm supposed to submit it:

done=FALSE
repeat
  A
  B
  if a then
    done=TRUE
  else
    C
  while b AND done NOT TRUE
D

I hope I'm not missing the mark by adding 'else' in there, since if done becomes TRUE it has to skip (exclude) C.

waldis 0 Newbie Poster

Thanks for your help, it starts clicking in, I just need to keep keeping on with these problems and one day it'll click in and stay ;)

Waldis ;)

waldis 0 Newbie Poster

Yup, I did miss out 'B' (I guess I should blame my keyboard, how bad it is, etc., but I won't)

What if 'c' is FALSE and 'd' is FALSE, it will go to 'B' insted of 'F' where it should...

Would this work:

A
while c AND not d
  B
  if not d then
    E
F

yeah, but if 'c' is TRUE and 'd' is TRUE, instead of going to 'B' this time it'll go to 'F'...

waldis 0 Newbie Poster

Could anyone look at this and tell me if I got it right? I had to rewrite the following code without GOTOs:

Y:  A
     B
     if a GOTO X
     C
     if b GOTO Y
X:  D

Here is my solution (I added an additional flag to get it done):

i=TRUE
while i
  A
  B
  if not a then
    i=FALSE
  else
    C
    if b then
      i=FALSE
D

Please correct me if I'm doing something wrong.

Thanks, Waldis

waldis 0 Newbie Poster

Ok, here is my solution for it:

A
i=TRUE
while c AND i
  if d then
    i=FALSE
  else
    E
F

All I had to do was add an additional flag where 'c' is, and if 'd' becomes FALSE, just skip 'E' and join the arrow going back to 'c', instead of going straight to 'F'.

I hope I'm making sense...

Waldis ;)

waldis 0 Newbie Poster

There were two problems at the beginning. The first problem was the code with GOTOs and the solution that I wrote was the second code I included. Then I have the second problem, for which I attached the scanned image of the flowchart I'm having problem with. What I'm talking about (with no C and D, only c and d) is this second problem from the flowchart.

And you said that the first problem, as I understood, is correct.

waldis 0 Newbie Poster

There is no 'C' or 'D', only 'c' and 'd', which are flags. A, B, E, and F are processes. When 'A' gets down to 'c' and flags it TRUE, it goes to 'B', if FALSE goes to 'F'. If we get to 'B' the next stop is a flag 'd', and if 'd' is TRUE it exits to 'F', if 'd' is FALSE it goes to 'E' and from 'E' goes back to the flag 'c' for another round if 'c' gets flagged TRUE.
The instructions was to modify the flowchart so the pseudocode could be written without GOTOs. And as I see the problem here is either the flag 'c' on the FALSE side or the flag 'd' on the TRUE side.

I hope I'm making sense here... and thanks for bearing with me.

waldis 0 Newbie Poster

Thanks for your input.

Now I got completely confused... from c down ir TRUE, and from d down is FALSE.

By saying c after 'if not c then' do you mean setting c to TRUE? And by D you mean d, and setting it's value to TRUE?

You made my brain boil, but I didn't get anywhere...

Thanks for your help,

Waldis

waldis 0 Newbie Poster

Try this:
Open your PC, take our your sound card out and look for the card identifiers (like model number, manufacturer, etc.) go to google, type it in, and search for drivers, you should find it.
If the sound is built on board, make sure you install inf files (chipset utility) so the os can recognize what's on board, go to the board manufacturer's website and get the drivers for your board.

I hope it helps,

Waldis

waldis 0 Newbie Poster

Hi, could someone to direct me the right way with this? Here is the problem, which I think I have nailed:

before:

A
	 if c then goto X
	 B
	 if d then goto Y
	 C
X:	 D
	 E
Y:	 F

after (in pseudocode):

A
if c then
  D
  E
else
  B
  if not d then
    C
    D
    E
F

Now I have a trouble with the flowchart I have attached.

Here is a pseudocode that I figured out, and I know it is wrong:

A
while c
  B
  if not d
    E
  else
    need to break out of wile loop somehow to get to 'F'
F

Please let me know if the first problem is right, and if you could help me with how to tame the flowchart to get rid of the arrow from d to F...

Thanks in advance,

Waldis :confused: