Gonbe 32 Newbie Poster

It's more of a hint/possible solution towards what you need. As mentioned in the post it's something I quickly wrote down to show the idea of using a string you manipulate. It should work for an arbitrary bit count though, although I'm sure performance-wise it could be improved on.

You may use it, sure, but if you don't understand what's going on I don't see much of a point. I'll let you decide what you think is best for yourself.

-edit-

gonbe i tried to modify ur code for my problem but could't achieve, what i wanted

You should only have to change the define from 6 to 48. If that doesn't result in what you want I don't think I understand your problem correctly. If you set 48 it will take forever to finish, but that is to be expected with 2^48 strings that have to be calculated and printed. The logic of it should work. I'm not sure if I have time tomorrow to try to fix your existing code, it will probably be monday at the earliest if at any time. Try to figure out in the meanwhile what's going wrong and post your attempt(s). The bits increase, but that doesn't matter too much for the application's logic.

HunainHafeez commented: gonbe i tried to modify ur code for my problem but could't achieve, what i wanted, so if you can possibly make any changes within my code to make it work for 48 bits then please resolve it, i wasted a whole lot of time on it, Help would be appreciated de +0
Gonbe 32 Newbie Poster

Such C++ includes in C code. The way you do it will become problematic when using 48 bits as the amount of possible permutations is huge. You don't have to store them though and just output them as they get calculated. Another thing that might become an issue is the use of counters (depending on how you do it). You could use an unsigned long long which is guaranteed to be 64 bits but it's a C99 standard feature. I'd go for re-writing the alghorithm though.

Depending on what your goal is exactly you could use a string. A quick attempt using a recursive method would result in something like this:

#include <stdio.h>

#define BIT_COUNT (6)

void print_bits(int bitcount, char* bitstring, int offset)
{
    if (bitcount == 0)
    {
        printf("%s\n", bitstring);
    }
    else
    {
        bitstring[offset] = '0';
        print_bits(bitcount - 1, bitstring, offset + 1);

        bitstring[offset] = '1';
        print_bits(bitcount - 1, bitstring, offset + 1);
    }
}

int main()
{
    char bits[BIT_COUNT + 1];
    bits[BIT_COUNT] = '\0';

    print_bits(BIT_COUNT, bits, 0);

    return 0;
}
Gonbe 32 Newbie Poster

Look into WinSock when programming on a windows platform and BSD Sockets for Linux.

You can also use Boost.Asio.

Beej's socket guide is also a well-written source to learn how sockets work for a beginner.

iamthwee commented: +1 +14
Gonbe 32 Newbie Poster

Thanks jallen and Andrew ( for explaining it very clearly )

You should note that their answers are wrong though. The outcome of the expressions cannot be determined.

Lucaci Andrew commented: Indeed. +5
Gonbe 32 Newbie Poster

You can't say anything useful about the code, only that it results in undefined behaviour; you're writing to the same variable twice or more without an intervening sequence point.

See section J2 and section 6.5 here.

-edit-

Looked up the exact point in section J2 I wanted to refer to:

Between two sequence points, an object is modified more than once, or is modified
and the prior value is read other than to determine the value to be stored (6.5).

Then, from section 6.5 (expressions):

Between the previous and next sequence point an object shall have its stored value
modified at most once by the evaluation of an expression. Furthermore, the prior value
shall be read only to determine the value to be stored.

Then from Annex C one of the definitions of a sequence point:

The end of a full expression: an initializer (6.7.8); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (6.8.5.3); the expression in a return statement
(6.8.6.4).

Gonbe 32 Newbie Poster

I think the two pass solution is cleaner and would be easier to understand for a beginner.

Probably. Not sure about performance though especially if you want to consider rows not necessarily have the same amount of items. (You'd have to allocate every row size to the size of the row containing most elements. You'd then have to track the amount of occupied slots per row or work with sentinal values which might not also be possible. (although he does only mention positive integers)

To keep it simple, here's example code you can use to read in a file with the folowing format:

1859 1974 726 1684 3048 6243
1853 1896 763 1738 3298 6250
2011 1715 877 1647 3548 6250
2125 1769 648 1685 4213 6227
2047 1858 661 1681 4463 6247
2272 1839 486 1653 4713 6250
2219 1843 497 1616 4963 6175
1679 1420 1234 1901 5648 6234
1522 1851 1060 1817 5898 6250

More specifically the code assumes the following for correct functioning:

  • Every row contains an equal amount of numbers.
  • Every line ends with a newline. (to be more realistic I allowed the last line to not end in a newline)
  • Values are seperated by a single whitespace. (space or newline)

I did not assume the amount of rows is equal to the amount of columns as this is not the case in your example either. Many of these points are easy to adjust but they would make the code …

Gonbe 32 Newbie Poster

Hmm vague code is vague. I made a quick adjustment based on what i THINK you're trying to do here:

#include <stdio.h>
main()
{
    char a[4][4];
    int row,col;

    // Note that the body of this loop is executed only twice.
    // Is this what you want?
    for (row=0;row<4;row=row+2)
    {
        for (col=0;col<4;col++)
        {
            // Note that this condition is always true.
            // You're basically saying "something it equal to something else, or it isn't!"
            if ((row==col)||(row!=col))
            {
                //row index 0 and 2 get 'x'.
                a[row][col]='x';
            }
        }
    }

    // I assume you want to print the two rows you entered data for here.
    // (Why not do it directly, you wouldn't need the array? I assume you want
    //  to develop this further into something else..)
    // I modified the increment section. It goes through EVERY row now so not only the 2
    // you entered data for.
    for (row=0;row<4;row++)
    {
        // You only entered data for row 0 and 2.
        // So skipping the contents of the others now (while still printing the newline)
        if (row % 2 == 0)
        {
           for (col=0;col<4;col++)
            {
                printf ("%c",a[row][col]);
            }
        }

        // Note I added a newline as I THINK you want each row in the 2D array on it's own line.
        printf("\n");
    }
}

This would print the following:

xxxx

xxxx

If this is not what you want, provide more information..

Gonbe 32 Newbie Poster

One thing to look out for is integer division. the return type of fact, x and n are all integers. Try this:

#include<iostream>

using namespace std;
int fact (int no)
{
    int total;
    for (int i=0; i<=no; i++)
    {
        if(i ==0)
            total = 1;
        else
            total = total * i;
    }
    return total;
}
int main()
{
    int x,n;
    double result;
    cout<<"Please key i x value : ";
    cin>>x;
    cout<<"\n";
    cout<<"Please key in n value : ";
    cin>>n;
    if (x<n)
        cout<<"Wrong input, x valur must be greater than n !!\n"<<endl;
    else
        cout <<"\nResult = 1 +2!/"<<x-2
             <<" - 3!/"<<x-3
             <<" + 4!/"<<x-4<< " - "<<n
             <<"!/"<<x-n
             <<" = "<<1+(fact(2)/static_cast<double>(x-2))-(fact(3)/static_cast<double>(x-3))+(fact(4)/static_cast<double>(x-4))-(fact(n)/(static_cast<double>(x-n)));

    return 0;
}
Gonbe 32 Newbie Poster
do
{
    printf("|......|\n");
    printf("|..c...|\n");
    printf("|..ch..|\n");
    printf("|..chm..|\n");
}
while (0);
pooja.singh.3950 commented: thanks for helping me out and can u tell me this using for loop +0
Gonbe 32 Newbie Poster

Definately possible but quite challenging to find an optimal solution. Going to write down a quick first thought..

To make your example a little bit more complex say there's the following lines as well:

StateNumber  NextStateC
2            1
3            1

Given have the table filled already every branch of paths would resulting in building up it's own operand of an '+' (or).

From the start state you'd be able to deduct the following paths:

1 -> 2 : a
1 -> 4 : b

then you continue with 2 and 4. Information known about 2:

2 can reach 2 with b*.
2 can reach 1 with c.
2 can reach 3 with a.

This means an extension of our paths. First the directly described paths:

2 -> 2 : b*
2 -> 1 : c
2 -> 3 : a

After adding new paths you should always try to deduce new paths from them. in this case it would add the following. (you should deduct new paths after every deduction too, basically any path addition):

So adding 2->2: b* would result in: All paths that start with 2 get preceded by b* and all paths that end with 2 would be extended with b*. I reference paths in that's how you'll likely want your program to deal with it and it makes it easier for me to explain. The resulting paths after this addition are:

2 -> 2 : b*
1 …
Gonbe 32 Newbie Poster

Infix, prefix and postfix are merely notations. For example

1 + 2 : Infix, The '+' operator is written between the operands it applies to.
1 2 + : Postfix (Reverse Polish Notation), the '+' operator follows it's two operands.
+ 1 2 : Prefix (Polish Notation), the '+' operator preceeds it's operands.

Precedence rules ("order of operation") have nothing to do with ASCII values, as mentioned. These are just rules made to clarify unambiguously which procedures should be performed first in a given mathematical expression.

These rules are explained in many places, for example here: http://en.wikipedia.org/wiki/Order_of_operations

e.g. if you have something like the following:

9 / 2 + 5 * 7 

Precendence rules result in it having to be interpreted like this:

((9 / 2) + (5 * 7))

Operator associativity is also something to note. More on that could be found here:
http://en.wikipedia.org/wiki/Operator_associativity

In essence it determines how operators with the same precedence are grouped in the absense of parenthesis. For example:

1 + 2 + 3 + 4 + 5

Would have to be interpreted as the following because '+' is (generally) left-associative:

((((1 + 2) + 3) + 4) + 5)

If you're looking to parse mathematical formula's you might want to look at Dijkstra's Shunting Yard algorithm. Information on this can be found here:

http://en.wikipedia.org/wiki/Shunting-yard_algorithm

Gonbe 32 Newbie Poster

An easy approach would be to determine the mean in the first pass through the file, then go through the file again to determine the standard deviation by squaring the differences of every number with the mean and then take the square root of the average of those values.

There are also algorithms which calculate the standard deviation in a single pass ("on the fly") but those are more complicated and shouldn't be that difficult to find by a simple google search.

Gonbe 32 Newbie Poster

"Multiple switch" case, I am not sure what you mean by this. Checking for multiple cases is what you'd typically use a switch for so yes, multiple "case" statements are allowed.

Nesting a switch within a switch is also allowed. The switch body is just a normal or compound statement which can contain any other valid c labels. In fact, you could also use case statements there without a switch.

A little example:

#include <stdio.h>
int main()
{
    int a = 1, b = 2;

    switch(a)
    {
        case 1:
            printf("Falling through..\n");

        case 2:
            printf("Falling through..\n");

        case 3:
            printf("Falling through..\n");

        case 4:
        {
            switch(b)
            {
                case 1:
                    printf("b is 1.\n");
                    break;

                case 2:
                    printf("b is 2.\n");
                    break;

                case 3:
                    printf("b is 3.\n");
                    break;

                default:
                    printf("b is undefined!\n");
                    break;
            }
        } break;

        case 5:
        {
            case 6:
                printf("a is 6!\n");
                break;
        }

        default:
            printf("At the bottom.\n");
    }

    return 0;
}

When run, it will output:

Falling through..
Falling through..
Falling through..
b is 2.

because 1 is one, but the case bodies of the other switch don't break out of it using 'break'. As a result the case statements are executed in order. This is called "fallthrough".

At some point the body for case 4 is reached which contains a new switch statement. This is valid. For example purposes I DID add 'break' statements for every case now which is why only the following is shown:

b is 2.

Note that a break will only break …

Gonbe 32 Newbie Poster

I didn't really go over the code yet but how does it run for you when you change

struct node *list1, *list2;

to

struct node *list1 = NULL, *list2 = NULL;

?

Gonbe 32 Newbie Poster

Start simpler. Create a function with the following signature:

void repeat_character(const char symbol, const int amount)

what it should do is it prints 'symbol' to stdout 'amount' times. So, print_character('2', 5); should print

22222

Once you have that solving your original problem should be easier. If you don't know how functions work yet you cuold either read into it, or you could fill in the following section between parenthesis:

void repeat_character(const char symbol, const int amount)
{
}

as if you were coding in 'main' where 'symbol' and 'amount' can be used without having to declare them in the body or assign values to them. Once you've managed to build this you could post it here and we could proceed with the rest.

-edit-
You can modify the function signature I suppose (e.g. non-const 'amount' or using an unsigned integer) though it's goal will be the same. It will make your initial problem trivial.

iamthwee commented: Much better advice +14
Gonbe 32 Newbie Poster
    char *p1="Name";                // Non-const pointer pointing to non-const char.
    const char *p2="Name";          // Non-const pointer pointing to constant char.
    char const *p3="Name";          // Alternative syntax for p2.
    char *const p4="Name";          // Const pointer pointing to non-const char.
    const char *const p5="Name";    // Const pointer pointing to const char.

    ++p1;   // Fine:  p1 isn't a const pointer.
    ++*p1;  // Fine:  p1 points to chars who aren't const.
    ++p2;   // Fine:  p2 isn't a const pointer.
    ++*p2;  // Error: p2 points to a const char and you're trying to increment it here.
    ++p3;   // Fine:  p3 isn't a const pointer. (p2 and p3 are basically declared the same way but in a different syntax)
    ++*p3;  // Error: p3 points to a const char and you're trying to increment it here.
    ++p4;   // Error: p4 points to non-const data, but you're trying to modify the pointer itself here, which is const.
    ++*p4;  // Fine:  p4 itself is const but the data is points to is non-const. Dereferencing and then incrementing is fine as a result.
    ++p5;   // Error: p5 is a const pointer as well as it pointing to const data.
    ++*p5;  // Error: p5 is a const pointer as well as it pointing to const data.

Added some comments with each statement that should answer your question.

Uhm, I mean, here's a hint! It has to do with asteriks and the position of the const keyword! More precisely you should look up the difference between "const char" and "char

Gonbe 32 Newbie Poster

As far as I know Volatile is used when you don't want the compiler to perform certain optimizations with regards to value changes. (It is as if you say: "The value of this variable may change, even if this doesn't happen in the current program". This may happen in embedded systems for example where this shared memory occurs more.

A simple example would be:

int main(int argc, char* argv[])
{
    int* test = (int*)0x1234;

    while(*test == 2)
    {
    }
}

In this program the value at address 0x1234 will be obtained and checked if it matches 2. If not, the body of the while loop will be executed, but it's empty. When the compiler optimizes (don't know if this goes for all), it doesn't actually check the value at 0x1234 again, but assumes it has not changed. (because there was no code that changes the value there) In case another proces changes the value at 0x1234 that this code will make use of, you can use volatile.

Gonbe 32 Newbie Poster

What you are doing now is trying to use a functor. (overloaded () operator for that object)

Use the initialization section of the constructor to initilize the object:

Desktop::Desktop() : otwarte_okna()
{
    x1=y1=0;
    x2=y2=1000000;
}
Gonbe 32 Newbie Poster

Alternatively you could set your condition as the loop condition. A cunstruction I would use would be a do-while setup, as displayed below:

int i;

do
{//return from the loop only when the input is zero
	printf("\n1: Stack1\n2: Stack2\n0: to return\n");
	scanf("%d",&i);
	
	switch(i)
	{
		case 1:		printf("1");								break;
		case 2:		printf("2");								break;
		case 0:		printf("Thank you for using the service");	break;
		default:	printf("Give a Valid Option");				break;
	}//end of switch - case
}//end of while
while(i != 0);