gerard4143 371 Nearly a Posting Maven

hi im vikram my question is
WEATHER A NULL CHARACTER OCCUPY ANY SPACE IN MEMORY IF YES THAN HOW MUCH SPACE IT NEEDED?

Why are you resurrecting an old thread(Oct 4th, 2008) and why the CAPS?

gerard4143 371 Nearly a Posting Maven

You actually got this to compile with a C compiler? How did you manage that? Your structure and array of structures should be set up like below.

struct menuItemType
{
    char *menuItem;
    double menuPrice;  
};
struct menuItemType order[10] = 	
			{
				{"Plain Egg",2.50},
				{"Bacon and Egg",3.45},
				{"Muffin",2.20},
				{"French Toast",2.95},
				{"Fruit Basket",3.45},
				{"Cereal",0.70},
				{"Coffee",1.50},
				{"Tea",1.80}
			};
gerard4143 371 Nearly a Posting Maven

Try looking at the code below..You should be able to finish it with this hint..

#include <iostream>

int main(int argc, char *argv[])
{
    int count_one = 1;
    int count_two = 10;
    for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < count_one; ++j)
        {
            std::cout << '*';
        }
        for (int j = 0; j < count_two; ++j)
        {
            std::cout << '+';
        }
        ++count_one;
        --count_two;
        std::cout << std::endl;
    }
    return 0;
}
gerard4143 371 Nearly a Posting Maven

The code below will demonstrate why pointer addition behaves this way

#include <stdio.h>

#define ARR_SIZE 4

int mya[ARR_SIZE] = {1233, 543, 567, 789}; 

int main(int argc, char**argv)
{
	int i = 0;
	int *iptr = mya;

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", iptr[i]);/*array notation*/

	fputs("\n\n", stdout);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "value->%d\n", *(iptr + i));/*add integral to pointer*/
	return 0;
}
gerard4143 371 Nearly a Posting Maven

hi navedalam, ur explanation is convincing but j is not integer POINTER it is integer variable. So, by ur explanation...

ptr=10. so, 10*4 = 40. Now, this 40 shud be added to 19 b'coz j=ptr+19. i.e 40+19=59.
Therefore, j's value shud be 59. Please give me ur view..

Thank you.

When you add a integral value to a pointer, you use the integral * unit size. Unit size in this case is sizeof(int).

So

j = ptr + 10

is really

/*pseudocode*/
j = ptr + (10 * sizeof(int));
gerard4143 371 Nearly a Posting Maven

It is not neccessary to initialise j there...it could be iniatialise in next line...that would not pose any problem.

I think I need to go to the optometrist....

gerard4143 371 Nearly a Posting Maven

Ya, but it must be initialise by a address location or NULL (which is nothing but zero)

I'm not talking about the pointer, I'm talking about the j integer which is uninitialized.

gerard4143 371 Nearly a Posting Maven

You should mark this a solved.

gerard4143 371 Nearly a Posting Maven

No, that might not be the reason. :icon_confused:

Its still unwise to use uninitialized local variables.

gerard4143 371 Nearly a Posting Maven

And your example

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

char * toString(char character)
{ 
	char *x =	malloc(2 * sizeof(char));
	x[0] = character;
	x[1] = '\0';	
	return x;			
}

int main(int argc, char**argv)
{
	int ca;
	char *ans = NULL;

	fputs("Enter a character->", stdout);
	ca = fgetc(stdin);

	ans = toString(ca);
	
	fprintf(stdout, "%s\n", ans);

	free(ans);
	ans = NULL;
	return 0;
}
gerard4143 371 Nearly a Posting Maven

My point on the comments refers to commenting something like this

char character = getchar(); //get a character from stdin

getchar() is a function from the standard library. Its well documented, so I consider commenting its functionality redundant. That said, if your Prof wants comments then give him/her comments.

gerard4143 371 Nearly a Posting Maven

Any memory that you allocate should be also freed and since your returning a pointer to the allocated memory, you can use that to free it.

Also, why all the comments its really distracting.

gerard4143 371 Nearly a Posting Maven

I don't quite get what you mean? Here is just one little example... it works as intented in my compiler. If you mean that this is too simple, then yes, you are right. If you want something more complicated, tell me and I'll try my best...

If you weren't mean, then I'm sorry.

What do I mean? People generally post technical questions here. If you want to post a code snippet then you should post it that way.

gerard4143 371 Nearly a Posting Maven

@gerard4143: writing makefiles is such a voodoo black-magic kinda thing. I wouldn't wish that on my worst enemy. In this case, either qmake or cmake are directly and very easily usable with Qt, that is by far the easier option, if Qt Creator really cannot be used to build a release version (extremely unlikely!).

LOL...I thought user would like a brief introduction into making makefiles. You know something that would help demystify that voodoo.

gerard4143 371 Nearly a Posting Maven

Are you one of them?

gerard4143 371 Nearly a Posting Maven

A single core CPU can only run one thread at a time, a multi-core CPU can run more than one thread at a time and because of this we could have two or more threads modifying the same data at the same time.

gerard4143 371 Nearly a Posting Maven

Why don't you write a program and see for yourself?

gerard4143 371 Nearly a Posting Maven

You could use

buf = const_cast<char*>(ConstBuffer);

but using a cast to get your code to work is a good indicator of poorly written code.

gerard4143 371 Nearly a Posting Maven

Use the function fgets().

char *fgets(char *s, int size, FILE *stream);

gerard4143 371 Nearly a Posting Maven

Ahh yes. I get it now. So the real question is: Is it possible to pass the data from an object (that is pointing to protected data) down to a child class? I suppose I can create an intermediary variable and then just delete it, but that doesn't seem very efficient.

I don't see what the problem is. Just create member functions that get and set the values of the objects and pass the values with them.

The vector question.

std::vector<double> some_function(double d);
gerard4143 371 Nearly a Posting Maven

I think your mixing up terminology. What your doing in your program is not inheriting the value from a parent class. What your doing is getting/setting the values of two independent objects..they don't share any common data. So setting one to 34 will not change the other to 34.

gerard4143 371 Nearly a Posting Maven

A multi-thread program that's shares common data amongst the threads can run bug free on a single core CPU but fail on a multi-core CPU..How do you correct this situation? You anticipate and correct the possibility of two or more threads writing/reading common data by using a mutex or semaphore.

gerard4143 371 Nearly a Posting Maven

I looked your code over and....The reason child 'doesn't' inherit from mother is your working with two independent objects. MObj and CObj are two different objects with unique data members. You can't expect values in MObj to be reflected in CObj.

gerard4143 371 Nearly a Posting Maven

Make the data member public.

gerard4143 371 Nearly a Posting Maven

Because j is a local variable it takes whatever value that happens to be on the stack...To fix this initialize j to 0

int j = 0;
gerard4143 371 Nearly a Posting Maven

Try this

double sum = accumulate(a.begin(),a.end(),0.0);
gerard4143 371 Nearly a Posting Maven

try compiling with these switches

gcc filename.c -ansi -Wall -pedantic -o filename

This line

write(clientFd,sumOfPrimes(lowerLimit,upperLimit),200);

sumOfPrimes returns an int which you are saying is 200 bytes big...It should be

int ans = sumOfPrimes(lowerLimit,upperLimit);
write(clientFd, (const void*)&ans,sizeof(int));
gerard4143 371 Nearly a Posting Maven

A make file is just a list of instructions for the compiler and linker. If your interested try googling make file tutorial or check here

http://mrbook.org/tutorials/make/

gerard4143 371 Nearly a Posting Maven

I'm not sure what C compiler your using but you should use one that's modern..

these lines are wrong

read(clientFd,lowerLimit);
read(clientFd,upperLimit);
write(clientFd,sumOfPrimes(lowerLimit,upperLimit));

ssize_t read(int fd, void *buf, size_t count);
ssize_t write(int fd, const void *buf, size_t count);

Please note the number of arguments.

gerard4143 371 Nearly a Posting Maven

Can we see an example of the string?

gerard4143 371 Nearly a Posting Maven

Well I have the program set so that once it goes through a box it places a X in the square so that if it runs into another X it says that it is blocked. But do you understand how to change directions that the program looks. Like it checks the up direction first but then I am not sure about how to make the program check the next direction.

After a full night's sleep I realized your right. If you handle the logic correctly you can navigate a path with loops using the X out method.

gerard4143 371 Nearly a Posting Maven

What's your first problem?

gerard4143 371 Nearly a Posting Maven

Well I have the program set so that once it goes through a box it places a X in the square so that if it runs into another X it says that it is blocked. But do you understand how to change directions that the program looks. Like it checks the up direction first but then I am not sure about how to make the program check the next direction.

I still see a problem with your approach. Consider a path that has a loop in it, as your program walks through, one element a time X'ing out elements, it may inadvertently close the path prematurely, especially if you have to navigate through that loop to get to F. I would get this problem working for the simpler 'non-loop' path first and then look at a looped path.

gerard4143 371 Nearly a Posting Maven

Wouldn't it not matter if you are only checking the next square from the one that it is at?

Because your only checking the next character for possible path directions you'll end up in an infinite loop when you encounter a path that loops back onto its self. If you think about it makes sense, you found this path once and nothing has changed so when you loop back you'll travel around again and again.

gerard4143 371 Nearly a Posting Maven

Are you talking about a circle in the maze or in the program?

Yes, that circle makes solving your problem a very difficult task.

gerard4143 371 Nearly a Posting Maven

Your original posting had a loop in the path which made solving it difficult. Try tracing
through this array and you'll find you have a loop where I indicated.

000000000000000
011111111011110
010000001000010
011111001111110//loop ends here
010001001000000//loop
011111001000000//loop starts here
000100001111110
000100000001010
000111111101010
010000001001010
010000001001000
011100001011110
000100001010000
0S11111110111F0
000000000000000

gerard4143 371 Nearly a Posting Maven

If I was attacking this problem I would create two functions. The first one would find the starting position and the second one would be my recursive function.

Also, will the path through the array have loops in it?

gerard4143 371 Nearly a Posting Maven

I'm bumping this thread because I would like to see a solution for it..I thought it was a simple problem but then I realized that the array path had an internal loop which really complicates things.

So does anyone have any pointers on how to solve this problem?

gerard4143 371 Nearly a Posting Maven

Line 34 should be

writeRes = write(fd_out,genBuffer,bufsize);
gerard4143 371 Nearly a Posting Maven

First hint:

create an enumerated set like below

enum Direction {Left, Right, Top, Bottom};

Use these values to inform the recursive function where its being called from. This keeps the direction moving down the path and not doubling back.

gerard4143 371 Nearly a Posting Maven

Line 15

int input(worker_t*);
gerard4143 371 Nearly a Posting Maven

Try changing line 13 to

while(input != "done\n")
gerard4143 371 Nearly a Posting Maven

You have some fundamental problems with your code especially the lack of a proper formating scheme..Also please use code tags.

#include <iostream>
using namespace std;
int main()
{
int num;

cout << "Enter a two-digit number:\n";
cin >> num;

int ones_digit = num%10;//preform these operations
int tens_digit = num/10;//after you get the value for num...not before


if ((num>=20) && (num<100))//if statement was wrong
{
switch (tens_digit)
{
case 2:
cout << " twenty- ";
break;
case 3:
cout << " thirty- ";
break;
case 4:
cout << " forty- ";
break;
case 5:
cout << " fifty- ";
break;
case 6:
cout << " sixty- ";
break;
case 7:
cout << " seventy- ";
break;
case 8:
cout << " eighty- ";
break;
case 9:
cout << " ninety- ";
break;
default:
cout << " Error ";
}

switch (ones_digit)
{
case 0:
cout << " ";
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
default:
cout << " Error ";
}
}

if ((num >= 10) && (num <= 19))//if statement was wrong
{
switch (num)
{
case 10:
cout << "Ten";
break;
case 11:
cout << "Eleven";
break;
case 12:
cout << "Twelve";
break;
case 13:
cout << "Thirteen";
break; …
gerard4143 371 Nearly a Posting Maven

why do you use call input twice, using the increment?

He's showing you two equivalent ways of doing the same thing.

gerard4143 371 Nearly a Posting Maven

Okay I understand you're method. Just for future reference if I did know know how many elements I had (in this case I had 13).

Would I have to declare a pointer?

An array name is a pointer. You mean, can you dynamically allocate the memory? Yes but if the number of array elements remains constant its better to use a static array.

gerard4143 371 Nearly a Posting Maven

Sorry, I take that back; must only apply to 'c'; Just tired it on VC++6 and it worked.

Again, are you sure about your statement?

gerard4143 371 Nearly a Posting Maven

the for statement can't use floating point types as the variant. You are allowed integers and enumeration types.

Are you sure about your statement?

gerard4143 371 Nearly a Posting Maven

Your going about this the wrong way, to initialize an array you must access each element..Like so

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

int main()
{
	double l1=25.0, l2=5.0, l3=20.0, l4=10.0, x1;
	int array1[13];
	int i = 0;

	for(x1=0.0; x1<361.0; x1+=30.0)
	{
		array1[i++] = x1;
	}

	for(i = 0; i < 13; ++i)
	{
		std::cout << array1[i] << std::endl;
	}

	return 0;
}
gerard4143 371 Nearly a Posting Maven

You have a couple of problems in your code.

You should call the pow() like this

S=pow(M/I, M*Y);

result is used uninitialized here

S = result + 1 * D;
gerard4143 371 Nearly a Posting Maven

Please use code tags and proper formating.

The problem, don't define your function inside of main.