what is the command line to compile something? Or are you using an IDE? Do you know what operating system (e.g. *nix or MS-Windows)?
I use either g++ or make (to compile multiple files together). I'm programming this on a terminal on linux.
what is the command line to compile something? Or are you using an IDE? Do you know what operating system (e.g. *nix or MS-Windows)?
I use either g++ or make (to compile multiple files together). I'm programming this on a terminal on linux.
I'm not sure which thread library you are using, but even without it you are missing an #endif at the end of your header. I'm not sure if that's the main issue. Are there other errors besides the ones you pasted in?
I'm using pthread but no, I just forgot to paste in the #endif, it's there in my code. The errors I've posted are the only the ones I'm getting...
what compiler/version are you using ?
Not too sure as I am doing this on a university computer and I don't know where I can go check at this time.
...So I'm assuming that the reason for the while loop is to ensure that all of the data is read in - even if it exceeds the size of the buffer. But what do I know, I'm honestly surprised they even let me post in the C forum anymore.
Either way, good luck on your assignment. I have a similar project that I haven't started yet - setting up sockets in C with a client/server model is apparently somewhat difficult.
Thanks! :) And yeah, I'm actually having quite a hard time getting this right....
Also, you're most likely right since I received a bunch of gibberish from time to time now without the while loop... so how am I going to receive data if I don't know the exact character length I will be receiving?
I want to use the string class in my program but I keep getting the following errors:
process.cpp:10: error: 'string' has not been declared
process.cpp:10: error: prototype for 'Process:: Process(int, int, int)'
does not match any in class 'Process'
process.h:14: error: candidates are: Process:: Process(const Process&)
process.h:16: error: Process:: Process(int, char*, int)
process.cpp: In constructor 'Process:: Process(int, int, int)':
process.cpp:14: error: invalid conversion from 'intâ to âchar*'
make: *** [process.o] Error 1
I don't understand why I'm getting this since my code doesn't seem to differ from the examples I found in tutorials too much:
/*
* process.h
*
* Created on: Oct 13, 2009
* Author: NTUser1
*/
#ifndef PROCESS_H_
#define PROCESS_H_
#include "thread.h"
#include <string>
#include <iostream>
using namespace std;
class Process : public Thread
{
public:
Process(int at, string process, int bt);
int arrivalTime; // arrival time of a process
string name; // process name, e.g., P0, P1, P2
int burstTime; // CPU burst time of a process
int remainTime; // the remaining burst time of a process
void run();
private:
};
/*
* process.cpp
*
* Created on: Oct 13, 2009
* Author: NTUser1
*/
#include "process.h"
Process::Process(int at, string process, int bt){
arrivalTime = at;
burstTime = bt; //etc for the rest of the vars
name = process;
}
void Process::run(){
state = RUNNING;
sleep(burstTime);
stop();
}
I've just started started socket programming so there's a few things I'm wondering about:
I found a set of socket programming codes here:
http://cs.baylor.edu/~donahoo/practical/CSockets/textcode.html
And the thing I don't really get is here in TCPEchoClient.c:
while (totalBytesRcvd < echoStringLen)
{
/* Receive up to the buffer size (minus 1 to leave space for
a null terminator) bytes from the sender */
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
totalBytesRcvd += bytesRcvd; /* Keep tally of total bytes */
echoBuffer[bytesRcvd] = '\0'; /* Terminate the string! */
printf("%s", echoBuffer); /* Print the echo buffer */
}
Why did he use a while loop for this? Wouldn't you recieve the same data by just using
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
In the project I'm doing, I don't really know how long the string I will be recieving is so can I/should I just write something like
if ((bytesRcvd = recv(sock, echoBuffer, RCVBUFSIZE - 1, 0)) <= 0)
DieWithError("recv() failed or connection closed prematurely");
without the while loop?
EDIT: I tried it out and yeah, I recieved the same message with or without the while loop so my question still stands, why use a while loop?
But more importantly... the thing I'm having trouble with in my project is...
Okay, say I have a message to send like this:
char …
Yes.
Alright, thanks for letting me know!
pthread.h is generally for POSIX systems like FreeBSD, NetBSD, GNU/Linux, Mac OS X and Solaris, but Microsoft Windows implementations also exist. For example, the pthreads-w32 is available and supports a subset of the Pthread API for the Windows 32-bit platform.
If you are working in windows it wont work. U have to look for
pthreads-w32 .checkout the link
http://en.wikipedia.org/wiki/POSIX_Threads
Thanks for pointing that out. I tired compiling it on my school's Linux computer and there wasn't a problem. I'm not sure whether our instructor is okay with us using pthreads-w32 so I'll have to ask her about that...
Also, this is for another assignment but what about arpa/inet.h or sys/socket.h? Are those also only supported by unix-like systems?
How about you just ask your teacher since it will be easier and more straight forward.
Yep, I'll be seeing her today.
That sort of worked.... mutex.cpp doesn't have an error anymore but now I have an error message in mutex.h saying:
`pthread_mutex_t' does not name a type (on line 15)
as well as
pthread.h: no such file
Also, I forgot to mention that a Makefile is included with the codes as well but I don't know how to use it within Eclipse to build the codes. Do I need this Makefile or can I just build everything using eclipse?
The codes our instructor provided for us has been giving me a "no such file" error message for the line #include <mutex.h> in the following code:
/*
* mutex.cpp
* scheduling
*
*/
#include <mutex.h>
Mutex::Mutex()
{
}
Mutex::~Mutex()
{
}
int Mutex::Lock()
{
}
int Mutex::Unlock()
{
}
The mutex.h code it is referring to is here:
/*
* mutex.h
* scheduling
*
*/
#ifndef MUTEX_H
#define MUTEX_H
#include<pthread.h>
class Mutex
{
private:
pthread_mutex_t mutex;
public:
/** Initializing the mutex
*/
Mutex();
/** Destroy the mutex
*/
~Mutex();
/** Acquire the lock
* @return The return value of pthread_mutex_lock().
*/
int Lock();
/** Release the lock
* @return The return value of pthread_mutex_unlock().
*/
int Unlock();
};
#endif
I've double checked and both codes are in the same directory and just in case you need to know, I am using Eclipse C/C++ and MinGW. Hopefully someone can help me out here so I can actually get started on this assignment!
Alright, thanks for your help. :)
I didn't realize that I was using different command line arguments for A5Q5 and A5Q6 until now. So it seems that my code works fine for arrays of smaller lengths (such as 128) but not for arrays of larger sizes, say 16328.
I'm working on a quick sort algorithm using recursion but it's throwing a stackOverflowError. Here is the code:
public static void quicksort(int [] A){
quickSortRec(A, 0, A.length-1);
}
public static void quickSortRec(int [] A, int p, int r){
if (p < r){
int q = Partition(A, p, r);
quickSortRec(A, p, q-1);
quickSortRec(A, q+1, r);
}
}
public static int Partition(int[] A, int p, int r){
int x = A[r];
int i = p-1;
int j = p;
while (j < r){
if (A[j] <= x){
i++;
int tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
j++;
}
int tmp = A[i+1];
A[i+1] = A[r];
A[r] = tmp;
return i+1;
}
The thing that's puzzling me is there's no problems when I call for it from this piece of code:
public class A5Q5 {
public static void main(String[] args){
//Length of array is given by command argument
int length = Integer.parseInt(args[0]);
//100 arrays of variable lengths of random elements are created and then sorted
for (int i=0; i < 100; i++){
int[] arr = new int[length];
for (int j = 0; j < length; j++){
Random rand = new Random();
int n = rand.nextInt(65578);
arr[j] = n;
}
try{
Sorting.heapSort(arr);}
catch (EmptyHeapException ex){
System.out.println("Heap is empty, unable to sort array.");
}
catch (OutOfRangeException ex){
System.out.println("Error: Out of Range");
}
Sorting.insertionSort(arr);
Sorting.quicksort(arr);
Sorting.quicksortImproved(arr);
Arrays.sort(arr);
}
}
}
but a stackOverflowError is thrown when I try to call for it from this one:
public class A5Q6 {
public …
Ah, can't believe I missed that... Thanks for all your help. My code's running fine now. :)
I am trying to create a heap sort method using recursion but whenever one of my methods called maxHeapify is called, it would throw a StackOverflowError. I understand that this error will pop up whenever my application recurses too deeply but the problem is I don't see anything wrong with my maxHeapify method that would cause such an error.
Here's the code:
public class Sorting {
public static void maxHeapify (int[] A, int i) throws OutOfRangeException{
int heapSize = A.length;
if ((i < 0) || (i >= heapSize)){
throw new OutOfRangeException();
}
int left = (2*i)+1;
int right = (2*i)+2;
int largest = i;
if ((left < heapSize ) && (A > A[i])){
largest = left;
}
if ((right < heapSize) && (A > A[i])){
largest = right;
}
if (largest != i){
int tmp = A[i];
A[i] = A[largest];
A[largest] = tmp;
}
maxHeapify(A, largest);
}
public static void buildHeap (int[] A){
int n = A.length;
int i = (int) Math.floor(n/2) - 1;
try{
while (i > 0){
maxHeapify(A, i);
i --;
}
}
catch (OutOfRangeException ex){
System.out.println("Error: Out of range");
}
}
public static int deleteMax (int[] A) throws Exception{
int heapSize = A.length;
if (heapSize > 1){
int largest = A[0];
A[0] = A[heapSize -1];
heapSize --;
maxHeapify(A, 0);
return largest;
}
else if (heapSize == 1){
heapSize = 0;
return A[0];
}
else{
throw new Exception();
//throw new EmptyHeapException();
}
}
public static void heapSort(int [] A){
int heapSize = A.length;
if (heapSize > …
I'm having trouble with drawing squares on the x86 assembler using the XGA 8-bit linear frame buffer. I'm suppose to draw a large square containing a grid of smaller 12x12 squares using my own functions and this is what I have so far:
[SECTION .text]
;----------------------------------------------------------------------------
; code belongs in this section starting here
mystart:
mov ah, 0fh ; BIOS function - get current video mode
int 10h ; call BIOS video interrupt
mov [vidmode], al ; save the current video mode
mov eax, 4f02h ; SVGA function - set SVGA video mode
mov ebx, XGAMODE ; select XGA video mode
int 10h ; call BIOS video interrupt
mov ecx, 12
mov eax, xPos
sqrver:
push ecx
mov ebx, yPos
mov ecx, width
mov edx, eax
push eax
mov eax, ebx
mov ebx, XGAXRES
mul ebx
pop ebx
add eax, ebx
vloop:
mov byte [fs:eax], 15
add dword eax, 800
dec ecx
jnz vloop
add dword eax, 5
pop ecx
loop sqrver
mov ecx, 12
mov ebx, yPos
sqrhori:
push ecx
mov eax, xPos
mov ecx, width
mov edx, ebx
push eax
mov eax, ebx
mov ebx, XGAXRES
mul ebx
pop ebx
add eax, ebx
hloop:
mov byte [fs:eax], 15
inc eax
dec ecx
jnz hloop
add dword ebx, 5
pop ecx
loop sqrver
mov ah, 1 ; select DOS function - input character
int 0f1h ; call OS function to wait for key press
mov ah, 00h ; BIOS function - set video mode
mov al, [vidmode] ; restore …
I really wish you'd use <= when you mean an inequality, or else you'll be thinking in terms of substitutions and how to plug and chug a solution, rather than the facts you are writing on paper.
The honest truth is, I don't know when an inequality sign or when an equal sign should be used. I'm struggling to understand algorithm analysis. I more or less understand the examples in my text book but I'm pretty much lost when it comes it doing it on my own since I don't know when a substitution should be used or how to deal with loops and summations.
I don't quite get it. (j+1) is the number of time the loop runs and n is the number of elements you have in the array so wouldn't substituting the n in mean running the loop (n+1) times?
I need help with finding a function T(n) that describes the upper bound for the worst case running time of this algorithm:
1. maxS = 0
2. n = A.length
3. for j from 0 to n − 1 do
4. for i from 0 to j do
5. S = 0
6. for k from i to j do
7. S+ = A[k]
8. end for
9. if S > maxS then
10. maxS = S
11. end if
12. end for
13. end for
This is what I’ve got so far (This is all new stuff to me and I have no idea whether I’m doing it right or not):
I started counting the cost of basic operations in the body of the inner-most for loop, which are: 2 ops. for incrementing k and 2 ops. for S+ = A[k], making a total of 4. Since the loop body is executed (j-i+1) times, this gives me 4(j-i+1). In addition, the test condition is executed (j-i+2) times and k is initially assigned to 0, thus:
T0(n) = (j-i+2) + 4(j-i+1) + 1
= j-i+2+4j-4i+4+1
= 5(j-i)+7
= 5n+7
I substituted (j-i) with n since that’s the worst-case number of steps that can be executed by the algorithm.
Then I moved on to the middle loop, where I've calculated its running time to be:
T1(n) = (j+2) + i= ∑(from i=0 to j) (T0(n)+5) + 1
where the number of …
Sorry for such a late reply!
What I meant last week about the compiler only letting me use " was that when I have a sentence containg a word like can't, the compiler will only let me compile if I wrap the entire sentence it's in with " rather than ' such that
writeln("There can't be an operation sign at the end.");
Sorry if there was any misunderstanding!
And yes, my prof allows us to pass by reference in this assignment. I have a question though: what difference is there between using global variables and passing by reference? I tried using reference variables but I don't really see any difference.
I also have another question about loops. If I had a read outside the loop and say it read a 'y,' why didn't it just skip my
while legalinteger and not eoln(infile) do
loop and continue on with the rest of program? Isn't y not a legalinteger?
By the way, I did what you said and got the loop fixed up. The program seems to be working fine now. :)
Also, what is considered as learning to program badly and learning to program properly? Maybe it's because I'm a newbie programmer but I don't really understand...
I can't seem to find the edit button... but nevermind about what I said about ". I see what you mean, but strangely, the computer lets me compile the program.
Thank you very much for taking the time to analyze my code! :) I found the paper method very useful and have revised my code. But now, I'm having a problem with my while-loop under convert...
By the way, my prof wants us to code the program using only procedures and parameters so I can't use functions...:
procedure convert ;
begin
digit1:=ord(c)-ord('0');
read(infile,c);
legalinteger:= (ord(c)<=ord('9')) and (ord(c)>=ord('0'));
while legalinteger and not eoln(infile) do
begin
digit1:= digit1*10;
digit3:= ord(c)-ord('0');
digit1:= digit1 + digit3;
read(c);
end;
num_just_read:=true;
end;
The program compiles and runs but it freezes when I enter something like 10+2.
Regarding
writeln('There can''t be an operation sign at the end.');
The program actually compiles if I use " instead of '. It actually doesn't compile if I use ' instead.
Hello. I'm utterly stuck on this one part of my school assignment (for pascal). We are asked to make a calculator like program which calculates integer values only. The program has to read character by character from an input file and if something like an 'a' or # is entered, then an error message will popup telling the user they can't enter that in the input.
The problem I'm having right now is that I can't seem to properly convert the characters to integers and have them add/multiply/subtract/divide up with eachother correctly. If I input an equation with two numbers like: 1+2 or 120/30 or 1000+300 then it works just fine but if I go: 1+2+3 or 1+20+4 or 1+2+3+4 then I get crazy numbers like 1975 or 17245.
This is what my program looks like so far:
program bbb (input,output, infile);
const
blank = ' ';
var
infile : text;
c : char;
eqn, equation : string[20];
digit1, digit2, digit3, digit4 : integer;
previousnotblank, currentnotblank, legalinteger, previousnum : boolean;
{*****************}
procedure convert;
begin
digit1:=ord(c)-ord('0');
end; { convert }
{****************}
procedure multidigits;
begin
digit1:= digit1*10;
digit3:= ord(c)-ord('0');
digit1:= digit1+digit3;
previousnum:=legalinteger;
end; { multidigits }
{****************}
procedure multidigits2;
begin
digit2:= digit2*10;
digit4:= ord(c)-ord('0');
digit2:= digit2+digit4;
previousnum:=legalinteger;
end; { multidigits }
{*****************}
procedure convert2 ;
begin
legalinteger:= (ord(c)<=ord('9')) and (ord(c)>=ord('0'));
if c='-' then
begin
read(infile,c);
if (ord(c)<=ord('9')) and (ord(c)>=ord('0')) then
begin
digit2:=ord('0')-ord(c);
previousnum:=legalinteger;
end;
end;
while legalinteger and (c<>'+') do
begin
if legalinteger and previousnum then
multidigits2
else
if legalinteger then
begin …