Saturday, May 9, 2009

What is the source code to sort a linked list in C? Strings are what to be sorted.?

It's a linked list of structures with strings. The whole program is like a directory. The string is the name. We must sort it out alphabetically as we enter the data. Kind of frustrating that I have to do ask this.

What is the source code to sort a linked list in C? Strings are what to be sorted.?
Instead of sorting the list after entering the node, you must sort it while inserting the node. Whenever you add a new node compare it with all the nodes already present in the linked list and insert it accordingly. The "head" pointer points to the first node and the successive "next" pointers point to the nodes that are (alphabetically) smaller than the current node.





This is just kind of a rough algorithm. Try to code it, hope you got it.
Reply:When creating linked lists I make sure a prototypical comparison method exists. This method is generally customized for each type making use of the template.





Like strcmp() the method is intended to return less than zero if the left operand is less than the right, zero if equal, and greater than zero if the left operand is greater than the right.





Two sorting methods are provided (as overloads of each other.) One is the general sort which will do the basic ascending or descending values of the list. The other accepts a function pointer that allows for a custom sorting algorithm.





An example of a custom sorting algorithm is a randomizer; it simply returns one of the three expected returns at random. :-)
Reply:That is not easy. May be you can contact a C expert at websites like http://askexpert.info/

elephant ear

Which strings are best for the G and C Strings on a Cello?

My teacher recommends me to switch the lower strings on my cello because it doesn't match the sound well. I currently have Helicore. Which is the best or a good one to choose from?

Which strings are best for the G and C Strings on a Cello?
Personally I think Helicore strings are the best, but if your teacher wants youto get new strings you should try gettind Dominant strings





hope this helps


Strings in C++?

Why can't I compile this program in Turbo C++\


#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;strng.h%26gt;


void main()


{


string c;


cin.getline(c,10);


cout.write(c,10);


getch();


}

Strings in C++?
Fix the include statement.





#include%26lt;string%26gt;


using namespace std;





After doing that, the compiler error about 'string' being unidentified should go away.





Now you will get compile errors about using 'c' as if it were a char* in getline. It's not, it's a class basic_string and getline() wants a char*





Though basic_string has a .c_str() method to convert itself into to a const char*, that's not good enough and the compiler will still complain. getline( ) needs a non-const char*. Even if you forcefully cast it to a char*, you'll get an access violation at runtime.





To use getline, you'll need a char*. You can then assign that to the string. It's really not the best example of using a string class, because you're calling methods that won't take a string, and you're not using any of the cool stuff that the string class will do for you. This will compile and run, though:





string c;


char read[10];


cin.getline(read,10);


c = read;


cout.write(c.c_str(),10);


getch();
Reply:you mispelled string.





#include %26lt;string.h%26gt;





if that doesn't, then do it manually with


char mystring[10]
Reply://Ahmm it's the string...


//there is no string or boolean on c++


{


char c[100];//Or char *c;


cin.getline(c,10);


cout.write(c,10);


getch();


}
Reply:try loading %26lt;string.h%26gt;


(C++) How do i change C-style strings into string objects, and a 1d array into a vector?

I've written a program that uses C-style strings and one-demensional arrays, but i need to switch the C-style strings to string objects and the arrays into vectors. Is there some simple way to go about doing this? I'm not 100% clear on how to use string objects and vectors, but is there some way where I can just change the way the strings and arrays are written and basically keep the program I've written?

(C++) How do i change C-style strings into string objects, and a 1d array into a vector?
yes, it's quite easy actually.





When declaring an array say:


int myIntArray[10]; use


std::vector%26lt;int%26gt; myVector; and optionnally : myVector.reserve(10);


Even better: typedef std::vector%26lt;int%26gt; TIntVector;


TIntVector myVector;








Everywhere you were using the array replace by the vector.


and everywhere you were receiving an array, receive an TIntVector instead.





All access stay valid. So myArray[9] = 0; become myVector[9] = 0;





string can be initialized directly from the c string like this.


std::string someString = szSomeCString;


How do i read in arrays of numbers and streams, using c strings in c++?

Im writing a census program that requires me to use c string objects to read in three things, names(states), population, and area. What I dont know and cant find in my text book, at least in a clear way, is how to read numbers and names from a text file saved separately in my directory. My objects are being declared as ifstream and ofstream.

How do i read in arrays of numbers and streams, using c strings in c++?
If you mean how to read them from the text file, ifstream and ofstream work just like any other stream, such as cin and cout:





ifstream read("census.txt")


string name


int population,area


read %26gt;%26gt; name


read %26gt;%26gt; population


read %26gt;%26gt; area

lady palm

Getting input from a text file using c strings in c++?

How do i get input from a text file using c strings, not the standard string class. Is it possible?

Getting input from a text file using c strings in c++?
Sure, as long as you use the #include%26lt;fstream%26gt; header, and name the file you want to use as input. When you parse the data, just store it to a C-String, which as you should know is an array made exclusively of characters.


C++ help with strings?

What are the major differences between (C strings) and the (string) class?

C++ help with strings?
The difference between living in a universe with a sun and one with eternal darkness.





string class is


easily expanable


easily copyable


has built in equality operators


has a multitude of insert, replace, etc., methods;


has iterators and revere iterators


can be passed to many stardard library algorithms





just off the top of my head.


C++ strings?

Hey! do you know what is this instruction doing... I mean, the ++ after the string...





for (i=0;i %26lt; strlen(texto);i++)


contadores[texto[i]]++;





THANKS!

C++ strings?
Looks to me like you've got an array called "contadores" which is, if I'm not mistaken, "counters" in Spanish.





texto[i] is going to be the i'th character in the string.


if it's, say, an uppercase A, then the 65 element of contadores will be incremented (have one added to it)





Voy a tratar de traducir estos instrucciones a espanol:


Parece que tiene un array (?) que se llama "contadores"


El valor de texto[i] va a ser la character en posision "i" en el string. Si es "A" entonces el elemento en posision 65 va a ser mejorado por uno.





Hope that helps.





It's really hard for me to talk about programming in Spanish.
Reply:increase the char "contadores" at the postition specified by texto[i], increase only 1 such as from 'a' to 'b'. Just like that.





suggess you trying to see its result by using printf to show what happen before and after this instruction, you'll know how it works.
Reply:The ++ increments whatever is before it, the value in contadores[texto[i]] in this case.
Reply:The ++ in contadores[texto[i]]++ will increment the value that the array contadores holds. If contadores is an array of characters it will increment the value it hold - i.e. if contadores[texto[i]] = 'a', contadores[texto[i]]++ will turn 'a' into 'b'.
Reply:in this for loop, i is being start from 0 and incrementing by one(i++) so it must go to 1 ine next iteretion


but it will jump to 2 not to 1


because in next line( contadores[text[i]]++ ) you are incremneting again so it will skip one step so use i+1 instead of i++in next line.
Reply:so your program will find the frequency of the characters...


ie.,for example if, texto has "sriram" then your countadores will increment the respective (ascii) positions.





as my previous person answered...





thanks


C++ Strings?

I have to take in a a string and scramble it 5 times, using string methods. I have to separate it using 2 radnom num's, and into three parts. It won't execute for some reason. Any help will be appreciated. Here's the code:


cout%26lt;%26lt;endl;


cout%26lt;%26lt;"Enter a phrase to shuffle: ";


getline(cin,phrase_input);





for(int i = 0;i %26lt;= 5;i++)


{


beg = phrase_new.substr(0,random_one);


mid = phrase_new.substr(random_one,random_two - random_one);


endphr = phrase_new.substr(random_two,phr_len - random_two);





shuffled_phrased+= endphr;


shuffled_phrased+= beg;


shuffled_phrased+= mid;





}


cout%26lt;%26lt;shuffled_phrased;

C++ Strings?
first, I assume that somewhere you are copying phrase_input into phrase_new somewhere and just didnt show the code.





second, make sure your random_one and random_two are not greater than the length of the string phrase_new.





third, to loop 5 times, your "for" loop should be "for(int i = 0; i %26lt; 5; i++)"





fourth, you need to copy shuffled_phrased back into phrase_new at the end of your loop, so you can repeat the operations on it again.
Reply:Do you know how to use a debugger?


If not, I suggest you learn.





A debugger will help you to pinpoint a problem to a specific line of code. Learn to step through the code and verify it is doing what it is supposed to be doing.





Learn to use breakpoints and the call stack.

snow of june

C++ Strings?

Is there a better way to add the word "not" after the word "and" in the string "Pork and Beans"?





void replaceit (string %26amp;str, int pos)


{





str.insert (pos + 3, " not");





cout %26lt;%26lt; str %26lt;%26lt; endl %26lt;%26lt; endl;








return;


}





pos is equal to the position in which the word "and" was found in the string "pork and beans". Instead of pos + 3, how can I make it so that any length word would fit too? pos is equal to 5, btw

C++ Strings?
You can tokenize the string ( using strtok ) and then add the words back to an output stream ( ostringstream ) - while adding, you can insert the word "not".


C++ Strings?

Okay i would like to pull a computer ID from Net View using strings. What i have so far is:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;





char A;





using namespace std;





int main (){


system("net view") = A


string str1( A , 11, 4 );


}


however, I can't seem to store new view as a variable.

C++ Strings?
int main (){


A = system("net view")


string str1( A , 11, 4 );


}





you were having system(netview) get the empty variable A.. flip it around and it should work


C++ strings?

i'm trying to convert a string (baseStr) to an array (baseArry):





string Convert(string baseStr)


{


int baseLength = (size_t)baseStr.length();


char* baseArry = NULL;


baseArry = new char[baseLength];


strcpy_s(baseArry, baseLength, baseStr.c_str);


}





It compiles okay, but when I run it, it says that the buffer is too small. Did I allocate my memory incorrectly?

C++ strings?
change baseLength line to this


size_t baseLength = (size_t) (baseStr.length() + 1);





And Convert function should be


char *Convert(string baseStr)


and return baseArry as the result;