Can you be a little clearer about what you want to achieve.
Are you looking into reading an entire file and check how many times a character appears in the file and pupulate that into an array???
Can you please explain yourself better.?
Can you be a little clearer about what you want to achieve.
Are you looking into reading an entire file and check how many times a character appears in the file and pupulate that into an array???
Can you please explain yourself better.?
Its everywhere... do a little searching ok? we are not here to work for you. just do yourself a little googling.
Your code please???
On the other hand i like the idea of recursion. Gone were the old days where the bigest ram was 64mbit. Now we have better memmory and processor speed. I dont see any difference in doing a 2x for loops and or a while loop and the recursion. I think the main question is when to use recursion. There are some tasks recursion is the most simple and effective way to go, some nearly impossible without recurssion.
So from my part, i think recurssion should be used with causion just like any other loop but once you fully understand the power of recursion, its fun to do a whole bunch of operations with just a simple well thought lines of code. So given my vote, i will vote for recussion without looking back. ))
Nice one Deceptikon. The difftime fucntion is one of the tutorial i wanted to show.
Sure its all about having fun and sharing ideas. no matter how trivial it may be. Thanks for your versions. nice stuffs.
with your code... i think there was no need forint clocks_per_ms = CLOCKS_PER_SEC / 1000;
as theCLOCKS_PER_SEC
const value is 1000000
so dividing by 1000 is 1000.Therefore that could have been done like below without the division at all.
void ms_sleep(int milliseconds)
{
clock_t start = clock();
int clocks_per_ms = 1000;
while (((double)clock() - start) / clocks_per_ms < milliseconds)
;
}
What is you take about that?
To Deceptikon:
Yes you are right. The idea here was to give insight of how to write a sleep timing. clock_t
is also a good idea but as you said its about choice and intended operation.
To triumphost:
The C++11
is not strictly supported unless you indicate the falg for c++11 in the compiler& the linker. That is not currently fully supported universally. But this thread convenient sleep functions are around before C++11
in the boost libs. This tutorial was to give the dity idea how to write sleep on your own. This i intend to give other 4 ways in the coming tutorials. Its about people knowing how things work. Just as there is sort in the algorithm lib but a student can be asked to write their own sort.
To Mike_2000_17:
Yes the chrono
currently can do almost all what the C time
does. Check it up because its currently the most reliable according to C++ Gurus. The chrono comes with convenient classes like hours,minutes,milliseconds etc. however i think the best way is to mix the standard ctime and the chrono together.
I expect people to provide something ... their own style and way to share ideas on that level.
thank you. ))
To Decptikon .. There is no sleep function that is 100% accurate as with your extensive experience in c++ i know you have thought about this million years ago. As you know, the problem consists of various factors. 1. The processes at current operational activities.
2. The size of system memmory 3. The platform and the processor.
I think this is one of the main reason why there is no standard fucntion in the ctime lib.
However like Schol-R-Lea stated, time_t will work on all platform because its low level and built in C as you know.
But for a qucik and dirty stuffs this fuction is ok.
This is a basic CPP sleep fuction. As you can see CPP libs do not come with a time sleep/wait fuction to use
in your code/loop.
So due to boring jurney to Saint Petersburg, i decided to write something/code to give anyone who has been
wondering why CPP does not consist of that function.
The idea is nice because there are at least 5 ways to achieve this results. I always like the easy and straight
forward ones.
Have a look at the code below.
I will starting a QT GUI full tutorials in the next few days. Everything QT So watch out for more this year.
Happy coding.
i will give you a full code for $400. Deal?
Show some code then.
To add further information to JasonHippy's answer
just do cout<<3%5;
$500 in 20 minutes. Deal??
He must be very rich by now.
have a look at the setw() from the lib iomanip.
for(int c=0;c<10;c++){
cout << std::setw(c+5)<<c<<endl;
}
cout <<setw(25)<<"Merry xmas "; // simple alc (10*2)+5
//out put
0
1
2
3
4
5
6
7
8
9
Merry xmas
Why? for doesn't even require you to write a function.
Because it is robust, compact , no error prone, easy to read and efficient.
Thats according to the task before us currently.
Understood?
The for_each
and lambda is clearly superior in the solution of this task. plain and simple ))
I will bet on runtime error. Something like array overflowed or Segfault.
Like the Dragon said. post your error.
line 27 void openfile(ifstream &inFile)
but the operation of that funtion is a bit lame because the is no logic only passing the stream object only to be asked for such operation.
I will advice you put a bit planing in your code.
1. char *getFile name(char *filename)
for file name return char /*.
2. void openFile(char *fileName)
open file with the getFile returned data.
one thing you must consider is that your file operation is not global. Therefore you will need to close the file each time and flush the data.
My best advice is to replan and maybe consider a class. With that your code will look more portable.
For line 73 does nothing.
Go back to the drawing board. I always draw before i code.
$500 in 1 day plus support even in GUI.
What i provided was a simple style or sorting a data.
If that algorithm can be used to sort linklist without a problem. I as on about sorting style. This is also applicable with link list.
I assume you are not doubting that are you ?
I know you are not but trying to be witty.
Good one. ))
Just like i promised... This is the sort.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void bsort(vector<int> &v){
int temp,index = v.size();
while(index > 0){
if(v[index] > v[index-1]){
temp= v[index];
v[index]= v[index-1];
v[index-1]=temp;
bsort(v);
}
index--;
}
}
int main()
{
vector <int> vec ={10 ,9, 8, 7, 6, 10, 12, 16, 20};
bsort(vec);
for_each(vec.begin(), vec.end(),[](int c){ cout << c <<endl;});
return 0;
}
// out put
20
16
12
10
10
9
8
7
6
And the output.
You can change the sorting order if you understand the sorting trick.
Help yourself my friend.
This is not a working code. Sure but this example will sort for sure.
Once ii get home, i will post a working code about this style here.
You are doing everything wrong according to me.
Sorting is very simple once you understand how its done. Take a pencil and a paper and make a sketch.
you need a *temp
to hold on to the checks in the if statement.
example
node::sort(){
node *temp=0;
node *start=head;
while(start !=0){
if(start.data > start->next.data){
temp =start;
start = start->next;
start->next=temp;
}else{
start = start->next;
}
}
}
Writing from phone but i guess you got the idea.
Its not a working code. Its just to give you the idea.
This is done simply with the for_each loop plus lammbda.
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
vector <int> vec ={1,2,3,4,5};
for_each(vec.begin(), vec.end(),[](int &c){ cout << c+2 <<endl;});
return 1;
}
## out put
3
4
5
6
7
Happy? ))
I have the code ready... Let me know when you need it so i can post it here for you ok? Read the FAQ's first anyway ok?
declare the operator inside the class.
Then use bool Myclass::operator<()
Are you asking about IDE's or what?
Also goto is legal in c++, its not a good practice to use it at all. Debuging and teamworking will be a hell for both parties. Its one big comand you should stay away from. Why not simple recursive function or a while loop?
Dragon my answer was about not reading the file as they have done. I clearly stated that i will not recommend that. ))
Read the entire file to a string and use string lenght function.
This entirely depends on the size of the file.
Just another way. How ever you can know the file size without reading the file to do the count++;
I would rather not read the file only for its size. very counter productive for serious programe.
Can you even post your code and error? Show some relationship with your claim ok?.
for insrtion sort. you need to go like this.
psuedo.
void insertSorted( person *p){ //takes the node pointer person
person * newNode =p; //
person * curr = 0; // formality
person *worker = head; //
if(head ==0){ // check if there's no node
head = newNode;
}
else{
while(worker !=0){ // loop
if(worker ->data >= newNode->data){ // check for >=
break;
}
else{
curr = worker;
worker->Next= worker;
}
}
}
if(newNode == head){ // then insert
newNode->Next=head;
head=newNode;
}
else{
newNode->Next=worker;
curr->Next= newNode;
}
}
this is the idea mate. ))
Are you guessing?
Check the op's code before you comment. goto line 12-16. What do you see?...
Also you provided nothing new and no solution to the op. Check first. ))
I will use associative array or any think like a hash.
read in the string and insert it into the object.
pseudo
user dataType<string,int> foo;
string data;
getline(cin,data);
loop through;
for(int c =0;c< data.lenght();c++){
//now yu check to see if the letter has been entered before.
//in this case string is the key and int is the value.
if(foo.key(c)==c){
int v= foo.getValue(c);
foo[c]=v+1;
}
else{
foo[c]=1;
}
}
Got the idea??? simple
Take of the code from line 12-16 and use this
dont forgrt to include the algorithm lib
for_each(ptrList.begin(),ptrList.end(),[](int *c){ cout << *c-1;});
I got a nice class just for that.
Very easy.
So whats is your question??
I smell homework.
That code was from from your instructor for you to update.
what is your question....?
Are you for real..?
Next time show some code ok?int foo = sizeof(array)/sizeof(int);
Yes you did
Mate you miss the road. This is not java but CPP pot. Get out....
on number 74, we dont read data like that. it must be cin>> filename
Also this is purely a homework that you have not touched. Its partially given by your instructor for you to do the res so be kind and show us what you have included so far ok?
you can use assert to verify the datatype entered if its a type int or char then make your decision. Very handy .
Whats is your error and also what do you intend to achieve with your code. Give more information next time you ask for a help. We cant read your mind.
I meant you should divide your code iinto smaller functions. Thats more easier to read and debug.
Also on number 1. void ReadStudentData(ifstream& infile, StudentType*& student, int& numOfStudents)
whats the point of int &numOfstudents
as a reference if you just write to it on line 12 infile >> numOfStudents;
like this. If you ntend to write to it like this, then there is not need to even use it in the parameter at all.
I personally think that you need to design your functions very well...
Also like what mike has already stated, what you want to do and what your function is asking to do contradicts to each other.
code please.
I think its only polite for me to add this to dragon's.
You should have functions to handle your code as it is very unconfortable to go through your code.
its really not confortable at all.