abhimanipal 91 Master Poster

Yes I did. I just don't know how I would convert/implement that into my program to output random numbers.

You first have an array of ints. Size of this array will be the numbers of words that you want to display. You initialize all the elements of this array to 0. Suppose your data consists of 4 words that. You create an array of 4 ints all initialized to 0.
According to the algorithm, you now choose a random number between 1-4. Suppose you choose 3, you print the 3 word and you make arr[2]=1 ie this word has already been displayed should not be displayed again.
Now you choose a random number between 1-3. Suppose you choose 3, you will print the word corresponding to arr[3] (word corresponding to arr[2] is already printed, so you should not include arr[2] when you count upto 3)

I hope this make it clear

abhimanipal 91 Master Poster

Not stupid ... Just less informed than you

abhimanipal 91 Master Poster

I'm sorry, I'm still confused!?!

Did you read the wiki link given in the previous post ?

abhimanipal 91 Master Poster

Why are you declaring a vector of Persons as well as an array of Person ? You need to have one not both
The way you are storing the best friends name is wrong. As of now you have a string called best friend in the main function. The getline call stores the input in this string. I dont think this is what you wanted. Because of this the display function gives a seg fault

abhimanipal 91 Master Poster

You are using fread for reading from a file ... Check out the suggestions posted in the posts above

abhimanipal 91 Master Poster

You come out of the while loop when you hit a period(.) Maybe that's why
Also for comparing the chars of the string ...you want to use == not =

abhimanipal 91 Master Poster

The variable size .... You have not given it any value .... It should be initialized as the length of the string

abhimanipal 91 Master Poster

There are 2 -3 compile time errors in your code..

#include<iostream>
#include<vector>
#include<iomanip>
#include<string>
using namespace std;
class Person
{
private:
string name;

// Make this bestFriendName. You have used bestFriendName in the function definitions 

Person* bestFriend;
int count;
public:
void setName(string n)
{
name = n;
}
string getName()
{
return name;
}
void setBestFriend(Person *p)
{
// This should be p not b
bestFriendName = b;
}
Person* getBestFriend()
{
return bestFriendName;
}
void setCount()
{
count = count + 1;
}
void display()
{
cout << "name: " << name << endl;
cout << "bestfriend: " << (*getBestFriend()).getName() << endl;
cout << "popularity: " << count << endl;
}
};
int main()
{
string yourName = "";
string bestFriendName = "";
int number = 0;
bool done = false;

vector<Person>persons;
Person myPerson[20];
cout << "Enter name (-1 to stop): "<< endl;
getline(cin, yourName);
while(yourName != "-1")
{
// No need for this line. Memory is already allotted when you create the array of objects
myPerson[number] =new Person;
myPerson[number++].setName(yourName);
cout << "Enter name (-1 to stop): " << endl;
getline(cin, yourName);


if (yourName == "-1")
{
for(int i = 0; i < number; i++)
{
cout << "Enter my best friend of" << myPerson[i].getName() << endl; 
getline(cin,bestFriendName);
} 
}
}

for(int i = 0; i < number; i++)
{
myPerson[i].display();
}
system("pause");
return 0;
}

This should clear your compile time bugs... But there are logical bugs in your code. When I ran your code I …

e30rapidic commented: big help +0
abhimanipal 91 Master Poster

Dude the variable size is initialized to 0. You dont change its value any where in the program. Give it the value of string length at some point of the program

abhimanipal 91 Master Poster

I dont know if this is a stupid question, but if the language is not English then how do you expect to take user input ?


Well I got the answer for my question.. Apparently key boards/ emulation software for non english languages are a common thing

abhimanipal 91 Master Poster

Why do you need the function swap big ?
You do the usual if else thingy in the function reorder.
Anyway you have pointers to the variables a,b,c as function parameter. You do the if else thingy and put the values in correct order in the variables if else. Some thing of this sort
http://answers.yahoo.com/question/index?qid=20061012215313AAYk5BA

Does that solve your problem or am I missing some thing ?

But the if else code becomes more and more complicated as soon as the number of variables increase. In such a scenario qsort will be better

abhimanipal 91 Master Poster

If you the format of the data then you can use the fscanf function

If not use read to read the contents of the file into a char array and then implement a parser to parse the data

abhimanipal 91 Master Poster

I have modified your code a little bit .. It should work now

int main()
{
    int numOfStars = 0;
    int count;
    printf("Enter a value of N \n\n");
      printf("N=");
	  scanf ("%d",&numOfStars);
	  int i, k=0;

       for( ;numOfStars>0; numOfStars--)
       {
             for(i=0;i<=k;i++) 
            {
                 printf(" ");
            }
            k++;
                 
            for (count = 0;count < numOfStars; count++)
            {
                    printf("*");
            }            
            printf("\n");
        }
}

You were very close. Your only bug was that you were terminating the while loop for printing the spaces way too late

abhimanipal 91 Master Poster

Use code tags..... It makes it much easier for us to understand your code when you use code tags

When I compiled your code, I get an error on this line

if(Ranks[Deck[CardDrawCount] % 13] == 0 )

The reason for this is that this expression becomes (when CardDrawCount is equal to 0)

if("Ace"==0 )

which leads the compiler to assume that you are trying to compare a string to an integer which is not possible. This error repeats itself in many places.

abhimanipal 91 Master Poster

Dude Where are your code tags

I am reading your getCalendar date function to print the date in the proper manner, but just a quick question, cant you read the file line by line and into a char array and then use strtok with space as the separator ? From what I read in the problem statement and the way the input is given to you I am pretty sure it will work ...

abhimanipal 91 Master Poster

Line 14 of your code is

(strings, n, sizeof(char *), compareStrings);

What is the purpose of this line ?
From what I have understood of your code I think you are taking the input correctly. One way to out put the words randomly would be to use the method given in this link

http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Pencil-and-paper_method

Use the original method, it is quite simple

abhimanipal 91 Master Poster

@firstPerson

Where I have written swap the 2 variables that's where code has to be written to swap the 2 variables....I guess to remove ambiguity I should have written
write code to swap the 2 variables here

@am5a03

You dont want to break when the size=0, you want to break much before that ... Check out the code segment that I have posted.

abhimanipal 91 Master Poster

Maybe this is a stupid question but what will be the value of size when the entire page is read ? Wont it be 0.
Cant you use that check to determine when the page is read ?

Or am I missing some thing ?

abhimanipal 91 Master Poster

Probably some thing of this sort

void rev(int* arr, int lower, int upper)
{
     if ( lower >= upper)
        return;

     // Swap the 2 values
     rev(arr, lower+1, upper-1);
}

Where lower and upper are the smallest and the largest indexes of the array

abhimanipal 91 Master Poster

got it thanks again

Mark the thread as solved

abhimanipal 91 Master Poster

The print function should print the contents of the linked list. If linked list does not contain any elements nothing should be printed. If the list contains only 1 element then only that element should be printed. If the list contains only 2 elements then only those 2 elements should be printed. So on and so forth

abhimanipal 91 Master Poster

Your structure is inside the class .... So the struct cannot be accessed without the object of the class
Is it necessary for you to have the struct inside the class ? There are 2 simpler options

1. Avoid the concept of class all together. Just use structs
2. Dont use struct inside the class. Make the linked list from objects of the class

abhimanipal 91 Master Poster

Can you post little more of your code ... These are the things that I want to see ..... The way you have written the for loop and the way you have written the conditions in the if else statements ....

PS are you sure there is no infinite loops any where else ?

abhimanipal 91 Master Poster

@Banfa ...
When you calculate new number of trucks and load per truck why will you get a different value ?

abhimanipal 91 Master Poster

All the choice should be inside the for loop. From the code that you have posted, your for loop ends after the first if statement.

abhimanipal 91 Master Poster

This code segment assume that there is a struct called node, which has the data members called next. I dont think your original solution had these elements ... Did you modify the code segment to fit your solution ?

abhimanipal 91 Master Poster
abhimanipal 91 Master Poster

I dont think the logic used by you is correct . Check out the method shown in this link...... Its far more simpler as compared to your code

http://www.knowledgesutra.com/index.php/Data-Structures-Linked-List-Reverse_t52777.html

abhimanipal 91 Master Poster

Also for your program it would make sense to change the data type of choice to int as opposed to what you have now

abhimanipal 91 Master Poster

If you are looking for a direction to get started at least .... Try making a text editor first which has the basic functionality like Notepad ....
Once this is done (and you are still determined to move ahead) then you can think of more advance functionality

abhimanipal 91 Master Poster
abhimanipal 91 Master Poster

I have written a simple example which shows how you can use the void pointer to pass an integer value. Maybe this will be of help

void func(void* p)
{
     int *x= (int* )p;
     printf("%d\n",*x);
}

int main(int argc, char* argv[])
{
    int x=10;
    void* p= &x;
    func(p);
            
	cin.get();
	
	return 0;
}
abhimanipal 91 Master Poster
struct Foo
{
    char* value;
};

 // are a and b created in the data segment of the asm. The data is probably is the code segment
struct Foo a = {"Hello from variable a"}; 
struct Foo b = {"Hello from variable b"};

int main()
{
    // what will happen here:
    struct Foo* ptr = &a;
    free(ptr);   // This is will give you an error. The reasons are explained above

    // In the malloc call you allot some memory. The address of this memory is stored in bar
    struct Foo* bar = (struct Foo*)malloc(sizeof(struct Foo));

// Now you store the address of the variable b in the variable bar. The address of the previous memory is lost
    bar = &b;
    
// what happened to the the original contents of bar->value?They got lost
    bar->value = (char*)malloc(sizeof(char)*2);  
    bar->value[0] = 'H';
    bar->value[1] = 'i';

    free(bar->value);
    free(bar); 
 // This is an error because you are trying to free the memory in which the global variable b is stored. As explained above free should only be called when memory is obtained dynamically. You got memory dynamically but lost the address of that memory. So free will give you an error

    return 0;
}
abhimanipal 91 Master Poster

1. In the code that you have posted, you have defined the function read in the function main ..... Put the brackets properly

2. Check how you have called the read function. Is that the correct way to call a function

3. I would recommend changing the name of the function from read to read_file or something of that sort . Read is a in built function to read data from a file... You may get some messy errors later. Why take chance ?

abhimanipal 91 Master Poster

Sudoku is a 9*9 matrix ... That means there will be 81 entries in all.
Out of these 81 entries some of these entries are filled when the grid is presented to the user for the first time. Let this number be temp.

So now for the sudoku box to be complete the user has to correctly enter 81- temp entries.
So the check to terminate the game will be when the user has entered 81- temp correct entries .

abhimanipal 91 Master Poster

I do not think that the logic that you have written for your premium bill is correct. Probably your premium bill function should be some thing like this

bAmount= basic_cost ;
if( day_mins_usage > day_mins _free)
{
// Add the extra charges here
}

if(night_mins_usage > night_mins_free)
{

// Add the extra charges
}

I hope you understood the mistake

abhimanipal 91 Master Poster

In the sudoku game you have to check 3 things .
1. The number exists in the row
2. The number exists in the column
3. The number exists in the box

You have written the code for the first 2 conditions...
In my previous post I have given you the pseudo code to check for the 3rd condition

abhimanipal 91 Master Poster

There are tons and tons of books, forums devoted to answer both of the above questions....
Did you try to do a google search on this subject ?

abhimanipal 91 Master Poster

On line 51 why are you closing the server socket ? Should that not be like the last step of the program ?

abhimanipal 91 Master Poster

In the main function case 'r' you are returning the value from the function in the variable AmountDue but you are printing the variable bAmount

This is not what you have done in the next case

abhimanipal 91 Master Poster

The method used by you to check if the num exists in that particular row or column is correct (though inefficient )

Checking if the number exists in that box is a bit tricky ..... You want some thing of this sort

// All the boxes are 3 * 3 in size
When you input the row number there are 3 possibilities .

Possibility 1: Row_num is 1, 4,7 
You identify these cases because row_num % 3 produces the same result ie 1. If the row_num is in this case you have to consider the rows,
row_num, row_num +1, row_num +2
 
Possibility 2: Row_num is 2, 5,8 
You identify these cases because row_num % 3 produces the same result ie 2. If the row_num is in this case you have to consider the rows,
row_num-1, row_num , row_num+1

Possibility 3: Row_num is 3, 6,9 
You identify these cases because row_num % 3 produces the same result ie 0. If the row_num is in this case you have to consider the rows,
row_num-2, row_num-1 , row_num 

You do the same thing for col_num

Once you get the beginning and the ending of the rows and columns... Then it is easy

for(row_begin........ row_end)
{
     for(col_begin ........ col_end)
      {
             // Do the checking here

       }
}
jephthah commented: yeah, i like this guy. +7
rlhh commented: Great guy, been a great help and has great patience. :) +1
abhimanipal 91 Master Poster

Thank You

abhimanipal 91 Master Poster

Is this what you are trying to do ?
Evaluate an infix expression like (3+5)/(2+2)

You will have to convert it to post fix and then evaluate the postfix expression
http://www.google.com/search?hl=en&q=evaluating+infix+expression&sourceid=navclient-ff&rlz=1B3GGGL_enIN258IN259&ie=UTF-8

abhimanipal 91 Master Poster

I think the string myLine should have a size ....

abhimanipal 91 Master Poster

Check out this link ...
http://www.cplusplus.com/forum/beginner/7386/

I think this should solve your problem ....

abhimanipal 91 Master Poster

Also line 75- 79 check where you have put the ' { '
Is that the correct place ?

abhimanipal 91 Master Poster

Could you post the part of the code where you read from the file and then call the function

abhimanipal 91 Master Poster

You can use the fscanf method.

int buf1[100];

fp1=fopen("data.in","r");                                             // Open the file containing data in the input mode
if(fp1==NULL)
{
 printf("%s","Could not open source data file");            // If the file cannot be opened the program exits
 exit(1);
}
else
{
 while(!feof(fp1))
  fscanf(fp1,"%d",&buf1[i++]);						// Read the data into the array buf1	
 i--;
}

PS. I am not sure if this is the best way to do what you want . Can somebody please comment on the above code ....

abhimanipal 91 Master Poster

The logic that I would use for this problem will be as follows :

Input Array A[50]:
Temp Array B[50]

for(i=0;i<50;i++)
B= A - target

Sweep thru B and find the greatest -ve number and the smallest positive number . Let the indexes of these values be i1 and i2
A[i1] is the greatest number less than the target. A[i2] is the smallest number greater than the target

Adak commented: Sweet! +2 +2
abhimanipal 91 Master Poster

ok but how exactly can i check the 10X10 array?just run over it?with for loops?

10 * 10 array has 100 location. But I guess you are just going to be using 20 . So be careful where you store the numbers and remember to check only those locations
PS. I would rather use 2 arrays of 10 integers than a 10* 10 matrix.... I think it will be more simpler