actually, it printed it out just fine ....
and after all this, i didnt really even need it, as i needed to just count the times ...... well, blah ... oh well, at least i learned something .... kinda
actually, it printed it out just fine ....
and after all this, i didnt really even need it, as i needed to just count the times ...... well, blah ... oh well, at least i learned something .... kinda
hmmm, never seen that code
cuz i was just about to ask how can i get it to be more precise?
so far my code:
#include <iostream>
#include <math.h>
using namespace std;
double xyz[3];
double mark[3];
void markoff(double x, double y, double z)
{
mark[0] = y;
mark[1] = z;
mark[2] = ((3*y*z) - x);
cout<<"x = "<<mark[0]<<" y = "<<mark[1]<<" z = "<<mark[2]<<endl;
}
int main()
{
xyz[0] = 1;
xyz[1] = 1;
xyz[2] = 1;
while( mark[2] < (pow(10.0,101.0)) )
{
markoff( xyz[0], xyz[1], xyz[2] );
xyz[0] = mark[0];
xyz[1] = mark[1];
xyz[2] = mark[2];
}
return 0;
}
output:
-bash-2.05b$ ./a.out
x = 1 y = 1 z = 2
x = 1 y = 2 z = 5
x = 2 y = 5 z = 29
x = 5 y = 29 z = 433
x = 29 y = 433 z = 37666
x = 433 y = 37666 z = 4.89281e+07
x = 37666 y = 4.89281e+07 z = 5.52878e+12
x = 4.89281e+07 y = 5.52878e+12 z = 8.11538e+20
x = 5.52878e+12 y = 8.11538e+20 z = 1.34604e+34
x = 8.11538e+20 y = 1.34604e+34 z = 3.2771e+55
x = 1.34604e+34 y = 3.2771e+55 z = 1.32333e+90
x = 3.2771e+55 y = 1.32333e+90 z = 1.30101e+146
well, i get a whole bunch of those numbers, but no precision ...
grrrrrr .... and this isnt even for a CS class (discrete math 2)
oh ok...so 4.89281e+07 is 48,928,100 ?
thanks
ok, call me stupid, maybe i forgot my math:
but if a C++ program spits out this:
-bash-2.05b$ g++ markoff.cpp
-bash-2.05b$ ./a.out
x = 1 y = 1 z = 2
x = 1 y = 2 z = 5
x = 2 y = 5 z = 29
x = 5 y = 29 z = 433
x = 29 y = 433 z = 37666
x = 433 y = 37666 z = 4.89281e+07
what is the number? 4.89281e+07 ???
is it like scientific notation (4.89281 x 10^7) ??
ok, I am needing to calculate powers in C++ (ie, 10^6, 10^9, 10^101, etc)
the pow function uses double, and after 10^5, returns a number like (8e3245) ... but i need it for larger powers ...
now, is there a function I can do that will break it down somehow? my teacher said you can, and showed us an example, but the example was modpow (breaking down large numbers to be modded)
??
ive searched net, cant seem to find an asnwer
i have to do a program to do Markoff's Equation, and the limit is a large number .... i can do the markoff's equation already, just cant figure out a bound (ive bounded it in a lower number, but assignment requires a larger number that I cant do)
so any help on calculating the large power would be great ...
this isnt for his homework, i can vouch for that (he dont go to college or anything)! i dont know how to do this myself, so i have no help from me ....so i pointed him here..
so any help would be great .... its for a game, and on windows ...
yup, that was it ..... geeeeeeeeeeeeeeeeeez stupid me
hmm, i never thought of that ... ill go try that out ... stupid me ..
also, here is part of the output:
<div class="validate">
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10"
alt="Valid XHTML 1.0 Strict!" height="31" width="88" />
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
</a>
<br />
<br />
<a href="http://www.whiteazn.com/satworld/contact/feedback.php">
</a>
</div>
<div class="rowClear">
</div>
</div>
as you can see, i just want it to print on one line ...
like this:
<div class="validate">
<a href="http://validator.w3.org/check?uri=referer">
<img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict!" height="31" width="88" />
</a>
<a href="http://jigsaw.w3.org/css-validator/check/referer">
<img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" height="31" width="88" />
</a>
<br />
<br />
<a href="http://www.whiteazn.com/satworld/contact/feedback.php">
</a>
</div>
<div class="rowClear">
</div>
</div>
no...right now, if there are newlines or whatever, it prints them out .... but i dont want that
but i am skipping any extraneous whitespace
<img src= ..../>
turns into
<img src=..../>
this is the code:
(its not the most effecient, its just a basic program for a web design class to print out the tree structure)
#include<iostream>
using namespace std;
int main()
{
char currentChar, peekChar, prevChar;
int tabIndex, tabSize, innerLength, subIndex, numSpaces;
int i;
tabIndex = 0;
tabSize = 4;
innerLength = 0;
while (!cin.eof())
{
currentChar = cin.get();
if (currentChar == '<')
{
peekChar = cin.peek();
if (peekChar == '!')
{
tabIndex--;
}
if (peekChar == '/')
{
subIndex = 1;
}
else
{
tabIndex++;
}
if (peekChar == '\n')
{
cout<<"::::::::FOUND AN END OF LINE CHARACTER::::::::::::::::";
}
for (i=0; i<(tabIndex*tabSize); i++)
{
cout<<" ";
}
while(currentChar != '>')
{
cout<<currentChar;
prevChar = currentChar;
currentChar = cin.get();
if (currentChar == ' ')
{
cout<<currentChar;
while (currentChar == ' ')
{
currentChar = cin.get();
}
}
}
if (prevChar == '/')
{
subIndex = 1;
}
if (subIndex == 1)
{
tabIndex--;
subIndex = 2;
}
cout<<currentChar<<endl;
}
}
return 0;
}
i want it to print it without newlines:
<img
src="http....." />
is printed as
<img src="http...." />
so yea, im trying to get rid of any extraneous whitespace, and check for newlines so i can get rid of it and print on one line
is it just me or am i stupid? (save the comments har har)
im using C++ to pull in a character from a file ... (going character by character)
how do i check for a line feed, or return?
<img src="http://image.jpg" <-- return
alt="image" height="100" width="100 />
ive checked for currentChar == '\n', =='CRLF', every possibility...
i cant seem to find anything on the web ...
any help would be great...thanks
ok, im trying to figure out this simultaneous process assignment ...
i got it finished, runs fine for the most part, but 2 of the processes dont process....
the code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
int numof_bridgecars = 0;
int northID, southID;
//semaphore declarations
sem_t north_s, south_s, bridge_s;
//pthread declarations (23 total)
void *fnorth1 (void *arg);
void *fnorth2 (void *arg);
void *fnorth3 (void *arg);
void *fnorth4 (void *arg);
void *fnorth5 (void *arg);
void *fnorth6 (void *arg);
void *fnorth7 (void *arg);
void *fnorth8 (void *arg);
void *fnorth9 (void *arg);
void *fnorth10 (void *arg);
void *fsouth1 (void *arg);
void *fsouth2 (void *arg);
void *fsouth3 (void *arg);
void *fsouth4 (void *arg);
void *fsouth5 (void *arg);
void *fsouth6 (void *arg);
void *fsouth7 (void *arg);
void *fsouth8 (void *arg);
void *fsouth9 (void *arg);
void *fsouth10 (void *arg);
void *fsouth11 (void *arg);
void *fsouth12 (void *arg);
void *fships (void *arg);
int main(int argc, char** argv)
{
//pthread declarations
pthread_t tnorth1, tnorth2, tnorth3, tnorth4, tnorth5, tnorth6, tnorth7, tnorth8, tnorth9, tnorth10;
pthread_t tsouth1, tsouth2, tsouth3, tsouth4, tsouth5, tsouth6, tsouth7, tsouth8, tsouth9, tsouth10, tsouth11, tsouth12;
pthread_t tships;
void *thread_result;
int tot_ships;
int r1, r2, r3, r4, r5, r6, r7, r8, r9, r10,
r11, r12, r13, r14, r15, r16, r17, r18, r19,
r20, r21, r22;
int ifSemErr;
//initialize semaphores and check for errors
ifSemErr = sem_init(&north_s, 0, 1);
if (ifSemErr)
{
printf("sem_init() has failed\n");
}
ifSemErr = sem_init(&south_s, 0, 1);
if (ifSemErr)
{
printf("sem_init() has failed\n");
}
ifSemErr = sem_init(&bridge_s, 0, 1);
if (ifSemErr) …
is there a generic way of outputing to the screen (or maybe file) a tree?
i have a programmng assignment of which i have to choose 1 out of 4 possibilites for my HTML Web Design class ....
1 of them that im thinking is taking a html file (that is valid xhtml 1.0 strict) and outputting the html stree structure ....
i can already take the file, and split it up into its elements (tree branches) ... just, outputting it to display like a tree, im not sure how to take on that task ....
any basic example will help ....
sound like a hw assignment i had...
we had to program a "shell" to take in certain commands and display it to the screen ...
or this:
int main()
{
printf("sure, give me $40, and ill give you the wrong answer."\n);
return "$40 bucks for me!";
}
man, this might be hard for me to get across the question i have ....
i have this project that requires using semaphores to do a project. (i have it attached).
i am just looking for help on how to start it, or whatever.
im just trying to understand where to begin.
ive understand how to use semaphores in dealing with "Producer-Consumer" problem, where one process (producer) will write to a buffer, and the other process (consumer) will read from the buffer.
but here, i have 23 seperate processes that i will need to synchronize ....
the problem:
there is a drawbridge ... we are to control the drawbridge to all south/north bound cars to go across, and let ships go under .....
so 10 threads represent 10 North Cars ... 12 processes represent 12 South Cars .... and 1 Thread represents all the ships....
i know ill need a semaphore for the bridge .....
but i dont understand how i will queue all the processes ... assignment says to not use any queue whatsoever (as the semaphores will create a queue itself) ....
wait, i think i just answered the question .....
when the threads get created, it should check to see if the bridge is free (if the bridge is "locked"), if not, the thread should go to sleep (which in turn, creates the queue for me) ... then after each thread/"car" goes through the bridge …
just use fgets instead of scanf
>but its not compiling
I can't imagine why. Here's an idea, maybe it's because you can't seem to tell the difference between C++ and Java. :rolleyes:
lol...sorry, probably mean for me to be laughing, but i cant help it....
but yes, your getting your java and C++ mixed up..fix that and maybe that will help
well, that solved it.... im new to the whole pthreads ... i coerced the passed value wrong... needed to coerce it to int, which is what i thought i was doing ....
but now it works ...... thanks for the help!
n is a highly suspect name for a global variable. Did you accidentally declare a local variable n that was not an array? The global n would be hidden by the local n and you would get that error.
no, the only global variables i have are:
const int MAX = 1024;
int nums[50];
int tot_n;
my locals:
char readnum[MAX];
int x = 0;
int i, j, k, l, m, n;
char *ifEOF;
int numof_stgs;
int numof_threads;
pthread_t threadID;
void *thread_result;
i changed it since, and it worked...
but then, now im getting the error again elsewhere:
-bash-2.05b$ gcc project4a.c -lpthread
project4a.c: In function `numsort':
project4a.c:102: warning: assignment makes integer from pointer without a cast
project4a.c:104: invalid operands to binary -
project4a.c:108: array subscript is not an integer
project4a.c:110: array subscript is not an integer
project4a.c:111: array subscript is not an integer
93: void *numsort(void *arg)
94: {
95:
96: //compare and switch if needed
97:
98: int *index = (int*)arg;
99:
100: int last, tmp, next;
101:
102: next = index + 1;
103:
104: last = tot_n - index;
105:
106: if (last == 2)
107: {
108: if (nums[index] > nums[next])
109: {
110: tmp = nums[index];
111: nums[index] = nums[next];
112: nums[next] = tmp;
113: }
114: }
115: else
116: {
117: printf("checking last element\n");
118: }
119:
120: printf("\n%i: Hello World!\n", arg);
121:
122: pthread_exit ((void*)NULL);
123: }
im compiling a basic sorting algorithm that utilizies POSIX threads (pthreads) ....
everything was compiling fine, until i put the thread implementation in ....
all of a sudden, it gives me this:
-bash-2.05b$ gcc project4a.c -lpthreads
project4a.c: In function `main':
project4a.c:32: subscripted value is neither array nor pointer
project4a.c:33: subscripted value is neither array nor pointer
in regards to these lines:
32: n[x] = atoi(readnum);
33: printf("%i ", n[x]);
it is declared correctly, like this:
const int MAX = 1024;
int n[51];
int tot_n;
void *numsort (void *arg);
int main()
{
char readnum[MAX];
.
.
.
what error is that???? anyone?
ok, I have an assignment ...
it requires the use of pthreads ..
i pretty much understand it, well, sorta ....
the assignment is to read input from the command line (input redirection) from a file
the file contains a list of numbers, 1 per line, 50 total numbers max
we are then going to sort the numbers using the Odd-Even Transposition Sorting Algorithm.
so for each number we compare and sort, we are to use pthreads for each set of 2 numbers ...
anyone familiar with the sorting algorithm?
if not, let me know, and ill explain, but otherwise, ill default to the guess that you guys do ...
anyways ....
my question is, with pthreads, you have to create a PThread ID for each pthread call, right?
example:
main()
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int iret1, iret2;
/* Create independant threads each of which will execute function */
pthread_create( &thread1, NULL, print_message_function, (void*) message1);
pthread_create( &thread2, NULL, print_message_function, (void*) message2);
.
.
.
.
.
we declared 2 pthread ID's, "thread1" and "thread2"
this allowed me to create 2 different threads?
but in my assignment, we dont know how many threads we will be creating until we read in all the numbers ....
so im not sure how to declare all the pthread ID's I will need to declare?
any help/point-in-right-direction would be …
is there a site which will explain the sstream functions?
i have searched around, and havent found much ...
cplusplus.com doesnt have it?
this looks very familiar to me .... is this for a class? if so, which class? ...
ok, your going to have to bear with me (sorry, i feel like im running with my head cut off...hw assignment is due tomorrow, and yes, i should have started earlier) ......
i was getting each line, and then using the strtok function to break it up into each of its own ... and then compare the char and convert to int ..... didnt work ....
this is the code i had:
char input[200];
string tokout;
int num;
cin.getline(input, 200);
cout <<input<<endl; //prints out the whole line of numbers
tokout = strtok(input," \n\t"); //gets the first number
cout <<tokout<<endl; //prints the first number
while (tokout != NULL) //while loop to get the rest of the numbers
{
tokout = strtok(NULL," \n\t");
cout <<tokout<<endl;
}
cout<<endl;
it works, prints out all the numbers into the line ... BUT, when it gets to the end, it gives a segmentation fault ...
i thought when it couldnt strtok anymore (basically, reaches the end of the line), it returns a NULL. but i guess i cant compare tokout to NULL ?? (compiler says cant compare tokout to int, or im guessing its saying that i cant compare a string to a NULL ..
i did a similiar thing in C and it worked, but guess not in C++ ??
questions on stack overflow's code:
1. in your while loops, what if there is only 1 line, thus, no \n at the end? will it still process that …
also, we have to use unix redirection, instead of opening and closing a file..... this is my homework assignment, and he didnt specify what file he be using for inputting everything ....
ok...so then i can do this: ?
#include<iostream>
int main()
{
char input[200];
cin.getline(input, 200);
while (cin.good())
{
//modify the character line as needed
//(i will need to convert from char to int value)
//which i can use another while loop to pull the first character,
//get the digit, get the next character, get the digit, until
//i find a space ... then get the next number, until I find a NULL
//which means its at the end of the line (the getline() replaces
//the \n with a NULL ??
cin.getline(input, 200);
}
return 0;
}
??
ok...i am trying to figure out how to read input ....
i am reading input from the command line
sh% ./a.out < input_file
within the input_file, it looks like this:
0 4 6 7 3
1 3 6 7 4 3 6
2 3 6 7 3 2 4 6 7 7
3 5
4 3 2 9 1
and im looking to read in the whole thing .... in each line, the first number is the item #, and the rest of the numbers is some attributes for that item number ... and each line is a different item # ...
i know how to get it to read in the whole file:
while (!cin.eof())
{
//reads in everything until the end of the file
}
but how do i get it to read in until the end of the line (every line can have a different number of attributes)?
i was going to have another while loop within it?
here is some code:
#include<iostream>
int main()
{
int input;
while (!cin.eof())
{
//reads in everything until the end of the file
while (?????)
{
//reads in each line
}
}
do i read until i find \n ?
while (input != '\n')
??
any help would be great..thanks
hmm ... i was doing it in C++ ...i kinda understand C
this is the code i was trying to do:
here is the main file:
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <string>
#include "stackli.h"
using namespace std;
struct flight {
int num;
};
int main()
{
flight ted;
Stack<flight> readyq2;
int print;
ted.num = 1;
readyq2.push(ted);
ted.num = 2;
readyq2.push(ted);
ted.num = 3;
readyq2.push(ted);
ted = readyq2.topAndPop();
cout<<ted.num<<endl;
ted = readyq2.topAndPop();
cout<<ted.num<<endl;
ted = readyq2.topAndPop();
cout<<ted.num<<endl;
cout <<"hello"<<endl;
return 0;
}
here is the header declaration for the stack:
#ifndef STACKLI_H
#define STACKLI_H
//#include "dsexceptions.h"
#include <iostream> // For NULL
// Stack class -- linked list implementation
//
// CONSTRUCTION: with no parameters
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// Object top( ) --> Return most recently inserted item
// Object topAndPop( ) --> Return and remove most recently inserted item
// bool isEmpty( ) --> Return true if empty; else false
// bool isFull( ) --> Return true if full; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// Overflow and Underflow thrown as needed
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & rhs );
~Stack( );
bool isEmpty( ) const;
bool isFull( ) const;
const Object & top( ) const;
void makeEmpty( );
void pop( );
void push( const Object & x );
Object topAndPop( …
i havent been able to find anyone to help me ....
i am looking to create a queue ....
i cant use a stack, as that is LIFO (last in, first out) ...
but i need a FIFO (first in, first out) ....
i have some code for a stack class using linked lists .... and i tried modifying the code to do FIFO, but i keep getting an error in this one spot (instead of popping the head of the stack, i would have it traverse the stack to the back and return the end ... but getting the element before the end to point to NULL, well, keeps crashing the program ....
can anyone help?