I am researching about the Balloon Sort because it was one of my assignments, but the Google give me one Balloon Sort link sample only and the rest was Bubble Sort.
I compiled the code in Dev C++ and said that it has some error...
Here's [a link] (http://www.codemiles.com/c-examples/balloon-sort-algorithm-c-implementation-code-sorting-array-t10823.html) ! That Google gave me...
here is the code...
#include<iostream>
using namespace std;
void balloon()
{ int num, N[10], x, y, z,temp;
clrscr();
cout<<"How many number would you like to sort? ";
cin>>num;
cout<<"Input the "<<num<<" numbers:"<<endl;
for(x=0;x<num;x++)
cin>>N[x];
for(x=0;x<num;x++)
{
for(y=0;y<num-x;y++)
{ if(N[x] > N[x+y])
{ temp=N[x];
N[x] =N[x+y];
N[x+y]=temp;
}
}
cout<<"pass "<<x+1<<"] ";
for(z=0;z<num;z++)
{
cout<<setw(5)<<N[z];
}
cout<<endl;
}
}
Error Picture Link
Can you help me how to code the Balloon Sort in C++ with some explanations... Thanks in advance!
It will have some error because the compiler will search the int main() so change the void balloon, and remove the clrsrc();
Since you used the setw(), you must use the #include <iomanip>
The header is part of the Input/output library of the C++ Standard Library. It defines the manipulator functions resetiosflags(), setiosflags(), setbase(), setfill(), setprecision(), and setw(). These functions may be conveniently used by C++ programs to affect the state of iostream objects.
And lastly you must make the cout<<setw(5)<<N[z]; into cout<<std::setw(5)<<N[z];
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{ int num, N[10], x, y, z,temp;
cout<<"How many number would you like to sort? ";
cin>>num;
cout<<"Input the "<<num<<" numbers:"<<endl;
for(x=0;x<num;x++)
cin>>N[x];
for(x=0;x<num;x++)
{
for(y=0;y<num-x;y++)
{ if(N[x] > N[x+y])
{ temp=N[x];
N[x] =N[x+y];
N[x+y]=temp;
}
}
cout<<"pass "<<x+1<<"] ";
for(z=0;z<num;z++)
{
cout<<std::setw(5)<<N[z];
}
cout<<endl;
}
}
and if you run it... here is my sample output
How many number would you like to sort? 5
Input the 5 numbers:
8
2
4
9
0
pass 1] 0 8 4 9 2
pass 2] 0 2 8 9 4
pass 3] 0 2 4 9 8
pass 4] 0 2 4 8 9
pass 5] 0 2 4 8 9
--------------------------------
Process exited after 8.305 seconds with return value 0
Press any key to continue . . .
Hope this works on your assignment! Good Luck!
Related
I am trying to perform some operations on a text file containing a repetition of a C based string and some numbers. My code successfully carried out the operation on the first set but it would not get to the remaining sets.
Please see the content of the text file below:
Max Scherzer 2017
6.2 4 2 2 2 7
6.0 4 3 1 2 10
mod Cameron 2018
6.4 4 1 2 1 3
6.0 4 3 5 2 8
John Brandonso 2019
6.1 1 3 5 2 7
6.5 4 7 3 4 10
I have used .eof() and it completely messed up what i am doing.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
char playername [25];
int season;
ifstream gamefilein;
gamefilein.open("C:\\Users\\troy\\Desktop\\GAME_SCORE\\gameinfo.txt");
if(!gamefilein)
{
cout<<"unable to open file";
}
double IP;
int H,R,ER,BB,K;
int counter=0;
double totalscore=0;
while(!gamefilein.fail())
{
gamefilein.get(playername,25);
gamefilein>>season;
cout<<playername<<season<<endl;
cout<<"Game Scores:"<<endl;
while(gamefilein>>IP>>H>>R>>ER>>BB>>K)
{
int IPa=IP;
int IPb=(IP-IPa)*10;
int IPc=0;
if(IPa>4)
{
IPc=IPa-4;
}
int score=50+(IPa*3)+(IPb*1)+(IPc*2)+(K*1)-(H*2)-(ER*4)-((R-ER)*2)-(BB*1);
cout<<score<<endl;
counter++;
totalscore+=score;
}
cout<<"Number of Games Started: "<<counter<<endl;
cout<<fixed<<setprecision(2)<<"Average Game Score:
<<(totalscore/counter)<<endl<<endl;
}
gamefilein.close();
return 0;
}
I get the below result, but I want the same result for the rest of the information in the text file, for example, I am expecting two more results like the one I have below.
Max Scherzer 2017
Game Scores:
63
64
Number of Games Started: 2
Average Game Score: 63.50
Aren't you reading the file as a char array?
If I read this correctly you try to shift an int and double over a char array with numbers in a STRING right?
e.g. "6.2" string is different than a 6.2 double number in your memory, hence why it cant work.
You also seem to have a lot of spaces which should not forget as well.
Where do you get that string to begin with? I would recommend you change the creation of that file to a more convenient format e.g. cv or json
I just solved my problem myself. The problem occurred when the loop operating on the integers and double completes its run and sees the character-based string that is in the next dataset. So i inserted a clear member function just at the point where i check for end of file
(gamefilein.clear())
and that solved my problem.
Thanks for attempting to help
This question already has answers here:
How do I use arrays in C++?
(5 answers)
Closed 7 years ago.
I just started learning c++ and came to Arrays. So what I want to do is perform some function on the array that was passed in function. So for this I tried small example
#include <iostream>
using namespace std;
void printArray(int data[]){
cout<<"len is :"<< sizeof(data)<<endl;
for(int i = 0 ;i<sizeof(data); i++){
cout<<data[i]<<" ";
}
cout<<endl;
}
int main() {
int data[] = {1,2,3,4,5,6};
printArray(data);
cout<<"in main :"<<sizeof(data)<<endl;
for(int i = 0 ;i<sizeof(data); i++){
cout<<data[i]<<" ";
}
return 0;
}
And Following is the output I obtained from the code
len is :8
1 2 3 4 5 6 738192384 -1126994503
in main :24
1 2 3 4 5 6 738192384 -1126994503 0 0 -1061892411 32677 0 0 -1665163256 32766 0 1 4196851 0 0 0 463403231 1946389667
I am not understanding where the process goes wrong. or is some thing in my code that make these weird changes to happen. and also the sizeof() is giving two values for the same array.Correct me where I am wrong anywhere.
I am using Eclipse software with c++ plugin added.
Thanks in advance for help!!
sizeof doesn't work if you pass an array to the function in this fashion. Try passing the size with the array or pass the begin and end pointers of the array.
If you don't mind templates, you can pass the array by reference:
template<unsigned length>
void printArray(int (&data)[length]) {
//length is the length of data
}
Please explain me how the code i am providing gives the output as :
1 2 3 4 5 6 7 8 9 10 11
#include<stdlib.h>
#include<iostream.h>
int main()
{
randomize();
int Num, Rndnum;
cin >> Num;
Rndnum = random(Num) + 7;
for (int N =1; N<=Rndnum; N++)
cout << N <<"";
}
Please explain me this code snippet
Well you are taking an input Num from the user and passing that to a random() function. You are then taking the returned value from that function and adding 7 to it and assigning it to Rndnum. Finally you are looping through from 1 to the Rndnum and printing of each of those numbers (1, 2, ...., Rndnum).
In the case of printing out 1 - 11 you must have gotten a return value of 4 from random(Num).
since I cannot see neither the function randomize() nor random(), I cannot tell you what they do but in this case the function call random(Num) gives back a 4, so Rndum adds up to 11.
Lastly, the for-loop repeates 11 (1 to inclusively 11) times and each time the output is the counter N itself.
So depending on what random does to your variable Num the number of iterations of your loop change.
Hope that helps!
P.S. If you want to look into random numbers in c++, take a look here
C++ rand
I realize that there are many questions with this title on SO, but all of the ones I've found did things like i = ++i or f(f(f(x))), neither of which are in this code. It's an attempt at a backtracking solution to this. I have some experience with C but I've just started trying to pick up C++, and I've been doing Codeforces problems for practice. The snippet below is the main body of the program. main, which I have not shown, deals with input and output. I used global variables here for weights, answer, and max_depth in the interest of keeping each stack frame of solve as small as possible.
The input that's causing trouble is weights = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and max_depth = 1000. When I compile this with g++ std=C++11 file.cpp, it gives "4 3 2 3 4 3 2 3 4 ... 3 2 1," which is the correct answer. When Codeforces compiles it, it gives "9 10 9 10 9 10 9 10...", which is incorrect. My guess is that the order that for(int i : weights) traverses the vector in is not defined by the standard, but even then, I don't see why it would make any difference. What am I missing?
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
string answer = "";
vector<int> weights;
int max_depth;
bool solve(int left_scale, int right_scale, int last_added, int depth){
bool is_left = (depth % 2) == 0;
int new_weight;
int weight_to_inc = is_left ? left_scale : right_scale;
int weight_to_exceed = is_left ? right_scale : left_scale;
if (depth == max_depth){
return true;
}
for(int i : weights){
if (i != last_added){
new_weight = weight_to_inc + i;
if (new_weight > weight_to_exceed){
bool ans = solve(is_left ? new_weight : left_scale,
is_left ? right_scale : new_weight,
i, depth + 1);
if (ans){
stringstream ss;
ss << i;
answer.append(ss.str() + " ");
return true;
}
}
}
}
return false;
}
void start_solve(void){
if (solve(0, 0, 0, 0)){
return;
}
answer = "";
}
(The full code that I submitted, if it makes any difference, is here.)
EDIT:
In case anyone stumbles on this looking for an answer to the Codeforces problem: the issue with this code is that "answer" is reversed. Changing answer.append(ss.str() + " ") to answer = ss.str() + answer is the shortest fix that gets it working.
Why does this C++ code give different output on different compilers?
It doesn't give different output.
When I compile this with g++ std=C++11 file.cpp, it gives "4 3 2 3 4 3 2 3 4 ... 3 2 1," which is the correct answer. When Codeforces compiles it, it gives "9 10 9 10 9 10 9 10...", which is incorrect.
I believe you are misinterpreting your test results on the codeforces server.
The correct answer is "9 10 9 10...".
The output of your program is "4 3 2 3 4 3..." on both the codeforces server and your local workstation.
So your algorithm is wrong, and the output of the program is consistent.
You are mixing up the two fields on the test results "Output" and "Answer".
Check your test results again.
#include<iostream.h>
#include<fstream.h>
ifstream f("date.in");
using namespace std;
int i;
int P(int a[100],int k,int max)
{
max=a[1];
for(i=2;i<=k;i++)
if(a[i]>max)
max=a[i];
return max;
}
int main()
{
int x,a[100],n;
f>>n;
for(i=1;i<=n;i++)
f>>a[i];
for(i=2;i<=n;i++)
a[i]=P(a,i,x);
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
}
My "date.in" file consists of the following :
12
4 6 3 7 8 1 6 2 7 9 10 8
As the title states, the program should modify the array from within the file such that each number has the maximum value found in the array up to, and including, the position of that respective number. I've gone through it a hundred times but cannot figure out what's wrong with my code.
When compiled, I get the following:
4 6 3 7 8 8 6 8 7 9 10 10
Any assistance would be appreciated.
int i;
Globals are usually a bad idea. Because this loop:
for(i=2;i<=n;i++)
a[i]=P(a,i,x);
and this loop:
for(i=2;i<=k;i++)
if(a[i]>max)
max=a[i];
are running "at the same time", and thus i in the first one is NOT counting from 2 to n properly, it's only actually getting the first index and then the even indexes. (Check your results, the even indexes are 100% correct: x 6 x 7 x 8 x 8 x 9 x 10). If you use counters local to each loop: for(int i=2; ... then this problem wouldn't be happening.
Also your entire design is slow. Not sure why you did it that way, because it can be done easily in a single pass: http://ideone.com/LmD0HX.
And use <iostream> not <iostream.h>. They're actually different files.