Infarction 503 Posting Virtuoso

Yes, you can build an OS in C or C++. Yes you can add a GUI to it (somehow, I'm assuming your OS has one). No, we aren't sitting around hitting refresh so we can answer every post withing the first couple minutes after it's submitted. Yes, it's possible that somebody might be interested in helping, but it is a vast project and requires a lot of study and a bit of experience to even be able to plan it out, much less implement it. Likely, they'd just be energetic youngsters like yourself. Consider spending a few years learning before you tackle it. Or, once you get pretty familiar with the language(s), you could also try to join some open-source projects to get some insight on how larger scale projects, including OSs, are written.

Infarction 503 Posting Virtuoso

1) Post some code. Unfortunately, crystal balls are a bit out of the budget, so we can't see what's wrong.

2) A 9-year old book is... well, almost antiquated. Consider getting a new one, since C++ has been standardized since your book was published. Some compilers may have problems with old code. If your compiler is nearly as old as your book, I'd recommend getting a new compiler as well.

Infarction 503 Posting Virtuoso

I would slightly argue against this last phrase. Let's take agriculture for example. If food stores and supermarkets were taken away (for whatever reason) do you think it would be possible for the current population to survive by farming/hunting/gathering? I would argue that these skills have been in time. Farming, for example, was generally lost during/directly after the Industrial Revolution.

I think that many people would starve to death because they would not be able to find or make food for themselves.

Also, I would argue that the human race is slowly losing its skill to survive when alone. For example, if the average human today was taken, and inserted in Africa, I feel he would not survive without the help of others.

Therefore, I think it could be said that yes, technology can reach a breaking point where further technology hurts the human race as a whole.

I would point you to a phrase I often encountered back in my high school civ classes: division (or specialization) of labor. Human civilization has come so far precisely because we don't need to have everyone working to procure food, clothing, and lodging for themselves (I seem to recall some estimate of hunting/gathering requiring about 20 hrs per week to maintain liveable rations). By having a group of people dedicated to those tasks, the others are free to build up new constructs (i.e. technology) to serve both in our procurement needs and in other desires.

I do think that we are …

Infarction 503 Posting Virtuoso

try putting cin.get(); before your return statement.

Infarction 503 Posting Virtuoso

-If u have a slower I/O unit, then can the CPU execute instructions while waiting on slower I/O?
-In this case, this type of optimization works differently for different I/O units...
-So why are there no smarter .exe files that orders the instructions for your I/O-usage?
-Whouldn't that be a great optimization?

You can make an I/O call and then continue to compute unrelated code. However, the case often arises where the unrelated code is completed before the I/O, and then the CPU is essentially waiting. Then again, with OS process scheduling, this is pretty minimal. On a related note, this was one of the main considerations for the Hyper-Threading design in P4 processors (where a second thread could be executed while the first was waiting for something).

& by the way why increasing to 64-bits? Why not increase the size of bytes with 1 bit? Is that so terrible? I think 64-bits is really terrible...
(Because it was increased because of bigger mem-space in the future, huh?)
I at least can't see any purpose in making 64-bit calculations...

Take a hardware design class. Sounds like it could be useful... a 33-bit design would be really messy to work with, and hardly of any benefit over 32-bit.

Infarction 503 Posting Virtuoso

You might reconsider using an int as your parameter type. You're limited to 10 binary digits, which leaves you with binary values from 0 - 1023 decimal (at least on my system). If that's fine for your needs, then so be it; otherwise you might consider using a long or using a string and parsing each character as a '0' or '1'.

Infarction 503 Posting Virtuoso

Also, you probably want to compare barometer to a range, rather than a specific value. Perhaps something like:

// assuming storm is low, and better weather is higher
if(barometer <= storm)
// ...
else if(barometer <= rain)
// ...
else if(barometer <= fair)
/* etc... */

The ranges will be determined as the lowest category the barometer value fits into, and by using else if you won't have conflicts.

Infarction 503 Posting Virtuoso

1) use code tags when you post code please. It preserves formatting and makes it easier to read.

2) When I ran it, it seemed to work fine.

3) couple notes about your code:

int base;  
  int sales;
  int salary;
  double commission;
 
  base = 200; // should just initialize variables
  commission = .09; // should just initialize variables
 
  while ( sales != -1 ) { //sales not yet initialized, I get a compiler warning
    cout << "enter sales, -1 to end :";
    cin >> sales;
    //static_cast should just be around sales, rather than (commission*sales) (also get a warning related to this)
    salary= static_cast< double >( commission * sales ) + base;
    cout << "salary is $" << salary << endl;
  }
Infarction 503 Posting Virtuoso

Since you didn't specify whether you understood, I'll try to explain it briefly. Say you have the following code:

class MyClass
{
public:
   int x, y;
   MyClass(int a = 0, int b = 0)
   {
      x = a; y = b;
   }
};

You can change the values with the following:

MyClass* m = new MyClass();
    m->x = 5;
    m->y = 4;

However, if x and y are private, you'll need something like the following:

class MyClass
{
    int x, y;
public:
    MyClass(int a = 0, int b = 0)
    {
        x = a; y = b;
    }
    void setX(int n)
    {
        x = n;
    }
    void setY(int n)
    {
        y = n;
    }
};

    // in code somewhere...
    MyClass* m = new MyClass();
    m->setX(5);
    m->setY(4);
Infarction 503 Posting Virtuoso

We can help you, but first you have to help yourself. Try to solve the problem first and when you get stuck, ask a question; we won't do your homework for you. If you get stuck with designing your solution, at least post some pseudocode. If your code isn't doing what you think it should, post it and we'll look it over.

Infarction 503 Posting Virtuoso

offhand I'd say it looks fine, though you may consider inputting each one and checking it separately. That way if the 2nd assignment is wrong, you don't have to input the other 3 again.

Infarction 503 Posting Virtuoso

do{
blah blah blah
}
while (yearcount!=5)
That should help you a little.

Unless I missed something, the loop will go as long as amount < famount , assuming amount is updated to store the amount after a given time period.

Infarction 503 Posting Virtuoso

Spacial here too, but like WolfPack, I'm pretty sure I got all but maybe 1 of the objective questions right (I figure the "choose which is least like the others is somewhat subjective). I was clicking through the ads and noticed a small "No Thanks" link in the corner of the screen after like 10 ads, though, which was a nice find.

Infarction 503 Posting Virtuoso

I have Made a new program..but still there is a error declaration...
i realy nid this program tomorrow so pls help me....re check

Why should we have to check your code? You should be able to tell if it works or not. And then if you have a problem, you can create a thread for that problem. If you want someone to do your testing for you, you better start making offers for payment.

That said, the code seems to work fine. At least, it compiles and runs correctly (though I only did 1 trial) on VS2005, though I was surprised by that. Few notes:

void main() // should be int main(), as per any standard of C or C++
{
    int line,c,n,x;
    void pasc(int); // don't declare a function within a function. 
                      // Declare it before main() instead.
    // ...rest of the code
Infarction 503 Posting Virtuoso

Let's just look through your code and figure out why it give a backwards result:

while((input % 10) != 0) {
number=input%10;
input=input/10;
switch(number) {
// cases...
}

Each iteration of the loop takes the least significant digit and reads that one to you. What you're wanting is to take the most significant digit and read that one instead.

That said, your condition for the while loop is flawed. If I were to put in 2050 as the number, the loop would never execute (2050%10 == 0). You could just change the condition to while(input != 0) or simply while(input)

Infarction 503 Posting Virtuoso

No. Unless 3n2/2 is supposed to mean (3n^2)/2, which is something of a deduction...

Let's work this out from the inside out, starting with printf("Hello World"); . This'll execute in (approx.) constant time.

Now we've got a loop. This loop will execute i times (0 to i-1). For each iteration of the loop, we'll execute the inner statement, which we noted is constant. Overall our time is a linear function of i, which we'll approximate as being i and drop anything else.

Now we've got another loop. This loop will execute n times, coincidentally providing the limit for the inner loop. The maximum limit for the inner loop is also n, and we'll use this in a second. So the outer loop executes n times, and for each iteration of that loop the inner one will execute as well, according to its own conditions. We can approximate that the inner loop will execute n times for each iteration of the outer loop. So, for each iteration of the outer loop, there's n iterations of the inner loop. There's n iterations of the outer loop. That's a total of n^2 iterations of the printf(), which would be O(n^2) in big-O notation. If you need the exact complexity, you'll have to work through it again tracking the details.

Infarction 503 Posting Virtuoso

That doesn't look right. Each of statements in the innermost loop take constant (or close enough) time. What will change the execution time of this code is the loop conditions, both of which depend linearly on n. As they're nested, you have a situation of:

Outer loop, executes in O(n)
{
  Inner loops, which executes in O(n)
  {
    Constant time statements O(1)
  }
}

If you put these together, step by step you get the following:
- A: statements that execute in constant time.
- B: a loop which executes A in O(n) time.
- C: a loop which executes B in O(n) time, which is already executing A in O(n) time.

So you have O(n) iterations on the outer loop, each of which runs O(n) iterations on the inner loop, each of which runs in constant time. Overall, that's O(n) * O(n), which is O(n^2).

Long winded, but hopefully got the point across. ;) And as a little tip, this sort of analysis is based mostly on loop conditions; you'll pretty much ignore the other stuff ('cept function calls which in turn contain loops).

Infarction 503 Posting Virtuoso

This sounds somewhat suspicious (very useful for a malicious program) but I could be wrong...

I believe you could have the program open a file in the target directory, named as you wish the backup to be called. If the file exists, nothing's left to do. Otherwise you'd have to know where the original was, and copy the file to your target file. I think that would work fine, but I don't know if your OS will lock the original executable on the hard drive (doubtful, 'specially if you're just reading it).

Infarction 503 Posting Virtuoso

you might look at strtok() though I don't know that the MSDN page is particularly easy to understand...

[edit:] if you need to use std::string, you could either use the .c_str() method or see if there's a split() function (I don't remember if it has one offhand).

Infarction 503 Posting Virtuoso

Okay, I've finished learning C++.

HAHAHA:lol:

What can I do with it? Specifically, I want to do some graphics stuff, anything beyond iostream and fstream. What comes after C++? I know it does more than this.

Come up with a project. Once you know what you want to write, either start writing a library for it (if one is needed), or find a library someone else has written and use that.

What exactly is an API? What does it do, how is it different from other things? Does it just let you access hardware abilities?

An API is an interface for (typically) a library. It provides certain functions for you to use, and then it does the nitty-gritty behind the scenes. You don't need to know how the nitty-gritty gets done, and the API won't tell you. It'll just do it.

I've heard of GUI programming, what is it (beyond what it stands for :rolleyes: )?

It's just like it sounds, making graphical user interfaces for your programs.

Infarction 503 Posting Virtuoso

floating point numbers always have a degree of imprecision. A floating point value will never be equal to a whole number. You should check to see if it is within an acceptable epsilon from a whole number (or fractional value, if such a situation were to arise). (loanYears <= floor(loanYears) + epsilon)

Infarction 503 Posting Virtuoso

Your code probably shouldn't compile. add(int, int) is specified to return an int, but doesn't, which should raise a compile time error.

Infarction 503 Posting Virtuoso

Error 500

I put in whoever replies to this... :P

Infarction 503 Posting Virtuoso

Dodgers --> fishing

Infarction 503 Posting Virtuoso

70 for me

Infarction 503 Posting Virtuoso

If you have someIstream >> someString it will read up to the first whitespace. Try getline(infile, name) after you get the 3 integers. (I haven't used C++ for a while, so I might be remembering getline wrong; if someone can confirm that I'd appreciate it.)

Infarction 503 Posting Virtuoso

SO YOU WILL NOT HELP NEWBIES:sad:

We will help newbies who show some effort, through code or otherwise. We will not write code for newbies. We will not do research for newbies. And, as Grunt pointed out, if you really are a newbie, you probably should not be expecting to write anything complicated yet...

Infarction 503 Posting Virtuoso

Please don't double post

Infarction 503 Posting Virtuoso

That error looks like you've got a Visual C++ project expecting a precompiled header (VC++ usually calls it "StdAfx.h" or something, IIRC). I havent used VC++ in a long time, but you'll probably have to recreate your project to not expect that. The code seems to work fine for me.

Infarction 503 Posting Virtuoso

use something like this:

switch(tolower(letter))
{
  case 'a':
    cout << "Alpha"; // or return, if this is just a function
    break; // don't need this if you use return above

and repeat for all the letters.

Infarction 503 Posting Virtuoso
switch(value)
{
  case possibleValue1:
    // do something
    break;
  case possibleValue2:
    // do something
    break;
  /* ... */
  default: // value wasn't any of possibleValue[1,n]
    // do something
}

Post some code, or some sort of attempt if you want more help ;)

Infarction 503 Posting Virtuoso

$$$ OIL $$$ --> ConocoPhillips (NYSE: COP)

Infarction 503 Posting Virtuoso

I think the problem is in this code

for (int die = 0; die < numDiceRolled; die++) {
                         outcome += ( rand.nextInt(sidedDice) + 1);
                         outcomes[outcome] += 1; 
                 }

You use outcome += (rand.nextInt(sidedDice) + 1); . I think it's the += thats killing you. Once you have more than a single roll, there's the possibility that outcome will be larger than the valid index range of the outcomes array. Then you get your exception. Try changing the += to just = and see how it works.

Infarction 503 Posting Virtuoso

You could use a do-while loop. I can make two separate assumptions on how the program should work if you use that.

a) The second number should only be a single digit. Enforcing this (if you so choose) would require extra code. You could then check to see if that single digit occurs in the first number.

b) The second number is again without limitation (obviously, it should equal to or shorter than the first, though it's still valid otherwise). You could then convert the two to strings (as per my first post or another method if you prefer), and check if the second string exists as a substring of the first (by taking the beginning n characters of the first number and comparing that to the second number, where n is the length of the second number). Then move to the next index of the first number and take a new substring.

These probably aren't the only ways to do it, but they're the most obvious ways to me.

Infarction 503 Posting Virtuoso

It would be nice if we knew what the problem was (saves us time figuring out where to start).

Infarction 503 Posting Virtuoso

First start out with a simple skeleton code:

class Main
{
  public static void main(String [] args)
  {

  }
}

Now, let's think about what we need to do.

We probably should prompt the user to enter the numbers (even though you know it takes 2 numbers, prompts make it look nicer). You can do this with a few lines of System.out.print("some text"); (you can also use println if you want a newline at the end).

We also need to get the user's input. Assuming you're using Java 1.5, this is pretty easy. You'll probably want to use the java.util.Scanner class and it's nextInt() method.

Then comes the tough part: checking if the second number is in the first. I'd do it by making two strings out of the inputted numbers (using Integer.toString(/* int variable here */) ) and then using something like firstNumber.indexOf(secondNumber) .

I recommend reading and getting used to the Java API. You can find each of the classes and methods I mentioned in there to make sure you know how to use them. Good luck with your assignment ;)

Infarction 503 Posting Virtuoso

basically, you should put code segments between [code] and [/code] tags. It looks like this:

Your code will be in here
    and formatting will be preserved
    so we can all read it

If you want to point out a specific line, you can use [inlinecode] and [/inlinecode], which looks something like this . Inline code doesn't look as good (imho) when it spans multiple lines.

Infarction 503 Posting Virtuoso

Yeah, you can use eof(). If you read the example in the link I posted, you might get duplicate data.

The problem with the code segment you just posted is that you're still not reading anything from the file. (You also have nothing to pass to characterCount() becaouse of it.) You could do something along the lines of:

string temp;
  while (inStream >> temp) // loop as long as you can read data from the file
  {
    for(int i = 0; i < temp.length(); ++i)
      characterCount(temp[i], letterCount); // pass each letter to characterCount()
  }
insamd commented: very helpful indeed, helped me through my problem +1
Infarction 503 Posting Virtuoso

This code does compile and generates an exe file...When I went thru disassembly, I found nothing useful in it...There were no nops in it. First of all our basic coding principles say that it should give a compiler error... compilers like msvc, gcc simply pass without giving any error or warnings..

I compiled to assembly and peeked at it too. The compiler just optimizes the NOPs out, so you pretty much get stack allocation and stack deallocation and that's it.

Infarction 503 Posting Virtuoso

Yeah, IIRC floating point values are always signed. I think some compilers accept long double as a type though. The OP probably meant to use unsigned long long , I'm guessing.

Infarction 503 Posting Virtuoso

I'd agree with Grunt. But to make my post somewhat meaningful, I though I'd also point out that you made b of type unsigned long double . I'm thinking that you don't want a floating point value if you're doing integer operations (especially if you're using bitwise operators).

Infarction 503 Posting Virtuoso

First off, lets look at two code segments. The first is your original while loop, the second is writeTotal.

/* ... */
  // the while loop:
  while (!inStream.eof())
  {
    writeTotal(letterCount);
  }
/* ... */
// writeTotal
void writeTotal(int list[])
{
  int index;
  for(index=0; index<26; index++)
    cout << static_cast<char>(index+static_cast<int>('a')) << "(" << list[index] << ")" << endl;
}

Your loop is based of whether you've reached the end of the file. If you have not, you call writeTotal. That outputs the total from the array. It doesn't change the file, so you keep looping (thousands of times, if you let it go that long).

Second, using eof() might not be the best way to check for file state (see http://www.daniweb.com/techtalkforums/post155265-18.html; and kudos to Dave Sinkula for posting it in another recent thread. ;))

[edit:] So I tinkered with the code a little bit to get it working, and there were some things I noticed. You don't initialize letterCount, so whatever values you get as your result will likely be on the order of -1203455802 or similar. That can be fixed by calling initialize() , or just having int letterCount[26] = {0}; and the compiler will autofill the array with 0.

Next, you never call characterCount. This is the critical part of your program. I'd recommend that you change it to accept a string instead of a character, but that's up to you. Either way, there's also no check in place to make sure the char …

~s.o.s~ commented: you are helpful, keep it up [~s.o.s~] +3
Infarction 503 Posting Virtuoso

(...) it run "almost" right... it's loosing every eleventh integer out of .dat file. (...)

This is because it reads the number, then the loop condition fails so it doesn't save the number. I'll let you figure out a way to fix that ;)

Infarction 503 Posting Virtuoso

~a flips the bits in a, but doesn't save the result anywhere. On the next line, a is still equal to 0xffff. 0x marks that the value is written in hexadecimal (simlarly, a number preceded by a 0, such as 024, is written in octal). Lastly, and unsigned int is the same size as an int, which is 4 bytes on a 32-bit CPU and 8-bytes on a 64-bit CPU; perhaps you were thinking of a short int?

Anyways if you run the following code, you should get output of "0xffff0000" (assuming 4-byte int)

#include<stdio.h>

int main()
{
  unsigned int a = 0xffff; // same as a = 0x0000ffff
  a = ~a;
  printf("%x\n", a);
  return 0;
}
Salem commented: good answer - salem +2
Infarction 503 Posting Virtuoso

I made a few changes to the code, mostly small ones, but here's what I've got atm:

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

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

void startnumber()
{
  pcount = 0; // makes more sense to initialize to 0 (IMHO)
  ncount = 0;
  f.open("P2355.DAT",ios::in);
}

void finish()
{
  f.close();
}

void getnumber(int &num, bool &endfile) // use references instead of pointers
{
  int tmpnum;
  f >> tmpnum; 
  while((!f.eof()) && (pcount < MAXLEN)) // MAXLEN - 1 will have you only use 9 spaces.  
                                         // If you used <= you'd need the -1.
  {
    buffer[pcount++] = tmpnum; // since pcount was init to 0, use post-increment
    f >> tmpnum;
  }
  endfile = ((bool)f.eof());
  if(!endfile)
    num = buffer[ncount++];
}

void printnumber(int num) // just take a value, not a pointer
{
  cout << num << endl;
}

int main(void)
{
  int num;
  bool endfile;

  startnumber();
  getnumber(num, endfile);

  while(!endfile)
  {
    printnumber(num);
    getnumber(num, endfile);
  }
  return 0; 
}

I made myself a file consisting of 35 lines, each containing the line number (1-35). Here's the output when I run the program:

1
2
3
4
5
6
7
8
9
10   // up to here is correct, but this is where the buffer is full and the rest is junk
10
11
0
0
0
0
-1208485396
0
-1208485272
134521200
134521283
134521296
0
0
0

Here's the main problem that I see: once the buffer is full, you read in …

Infarction 503 Posting Virtuoso

I'd first off recommend using return types instead of pointers. Handling pointers like that (imho) just makes the code messier than it needs to be. You could do it with references instead of pointers as well. And instead of keeping endfile as a variable, just use f.eof() .

I'd also suggest you try to design the program to not use global variables. While they do make it easier for this size of a project, using them is typically regarded as a bad thing. You could either wrap them in a class (as you attempted before), or pass them around as parameters. You could (and probably should) also make use of return types. For instance, if your functions used return types, you could have int getnumber(fstream& f); instead of void getnumber(int*, bool*); . This is a more intuitive design for the function as well ( getnumber takes a file and gets a number out of it; seems pretty straightforward, no?). And the fstream could be a local variable in main, rather than a global variable.

As a precaution, you should make sure the file is not empty when you get the first number. You have:

startnumber();
  getnumber(&num, &endfile);

  while(!endfile)
  {
    printnumber(&num);
    getnumber(&num, &endfile);
  }

whereas I would recommend something along the lines of:

startnumber();
  while(!f.eof())
  { // note: I'd assume that you'd use references and not need the ampersands
    getnumber(num, endfile); // endfile could probably be dropped too if you use f.eof()
    // previous line could also …
Infarction 503 Posting Virtuoso

The easiest way would be to put a big loop around the main part. You could have your menu contain an extra option to quit, and just keep doing calculations until they enter that option.

Infarction 503 Posting Virtuoso

But in small programm's like this doesn't it really matter, but in large programm's is it an bad habbit.

A bad habit is a bad habit, no matter what size the program is. Sure, it can be easier for a small program, but that doesn't change things. Same could be said for goto. ;)

Infarction 503 Posting Virtuoso
#include<stdio.h>
[B]int[/B] main() // main's return type is int
{
/* ... */
b= m%2; // I'm assuming you mean [B]n%2[/B]

while( b!= 0) // this will check if [B]n[/B] is even or odd. if it's even, the loop will be skipped.  See below.

The loop has to continue until the b==0.

You probably mean until n == 0. b is just the LSB of n, which could become 0 at any point during the operation. Also, if you've already done b = b%2 , doing it repeatedly will never change the value of b.

How do I have the b @ every loop recorded for the final output? Or do I have to include this in the loop :
b = b%2 .

You'll probably have this in the loop. See above about using n%2 vs. using b%2 (you'll probably want b = n % 2; ). And think about what you need to do to n as well.

But I still can't figure out a way to print all the b's @ every stage of the loop after the loop has ended.

Why not just print them inside the loop?

Infarction 503 Posting Virtuoso

I always install Apache on my machines, so I guess I'm hardly ever "offline" in that regard.