Sign in to follow this  
Eralp

Optimization C++

Recommended Posts

Hi Happy New Year To All! I want to do this project; project I already done and compiled my project, but when I upload it, It gives me an error that the execution took 2 seconds and need to be terminated.In my computer it took only 15 ms (website compiles it one a 700 mhz computer) I couldn't find ANY optimization to do. I passed the strings with reference(thats the only thing I could thinkg of) Can anyone tell me what to do? There is only one function and it takes the groupmembername as input and gives me the position of that member.(as you can understand) Here is my code.I've been trying to do something since 4 hours but nothing came up.
/*
ID: eralp_b1
PROG: gift1
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

struct groupmembers{
int money;
string name;
  };

int stringtonumber(string &name, int nop,groupmembers* grupuyeleri);


int main() 
{

int numberofpeopleingroup;
int m;


ofstream fout ("gift1.out");
    ifstream fin ("gift1.in");

	if(fin.is_open())

	/////////////////// inputal

    fin >> numberofpeopleingroup ;

	
groupmembers* grupuyeleri = new groupmembers[numberofpeopleingroup];


for(m = 0;m < numberofpeopleingroup;m++)
{
   fin >> grupuyeleri[m].name ;
   grupuyeleri[m].money = 0;

}


	/////////////////// inputal

    /////////////////// money verme
    string moneyyivercek;
	string moneyyialcak;
	int moneyyialcaklarinsayisi;
	int money;
    int kalan;
	int bolum;

burayadon:;

	fin >> moneyyivercek;
    fin >> money >> moneyyialcaklarinsayisi;

    if(moneyyialcaklarinsayisi == 0)
		goto atla;


    kalan = money % moneyyialcaklarinsayisi;
    bolum = (money-kalan) / moneyyialcaklarinsayisi;

    grupuyeleri[stringtonumber(moneyyivercek,numberofpeopleingroup,grupuyeleri)].money -= (money-kalan);
 

	for(m = 0;m<moneyyialcaklarinsayisi;m++)
	{
	fin >> moneyyialcak;
	grupuyeleri[stringtonumber(moneyyialcak,numberofpeopleingroup,grupuyeleri)].money += bolum;
	}
atla:; // if the nmbers of the people who will get money 0 than jump here cuz
       // I dont want a NULL division.
if(!fin.eof())
    goto burayadon;


    /////////////////// money verme





   /////////////////// outputver
for(m = 0;m < numberofpeopleingroup;m++)
{
	fout << grupuyeleri[m].name << " " << grupuyeleri[m].money << endl;
}
   /////////////////// outputver





delete [] grupuyeleri;

    return 0;
}

int stringtonumber(string &name,int nop,groupmembers* grupuyeleriz)
{
	int x;
	for(x = 0;x< nop;x++)
	{
		if(grupuyeleriz[x].name == name)
			return x;
	}

 return 100; // this cant happen
}



[Edited by - Eralp on January 1, 2008 4:57:50 AM]

Share this post


Link to post
Share on other sites
My guess is that it can't find gift1.in -- you do absolutely no error checking here.

Thus, when this occurs:
fin >> numberofpeopleingroup;

It doesn't actually give numberofpeopleingroup a value, and since you never initialized it to everything, it can be any, huge ginormaus value. This means this doesn't loop through, say, 10 elements -- but closer to millions, none of the iterations of which actually load anything from fin:
for(m = 0;m < numberofpeopleingroup;m++)


This kind of mistake is why I tend to prefer errors be reported using exceptions rather than flags. Before actually using fin and fout, check if fin.is_open() && fout.is_open().

Share this post


Link to post
Share on other sites
Quote:
Original post by MaulingMonkey
My guess is that it can't find gift1.in -- you do absolutely no error checking here.

Thus, when this occurs:
fin >> numberofpeopleingroup;

It doesn't actually give numberofpeopleingroup a value, and since you never initialized it to everything, it can be any, huge ginormaus value. This means this doesn't loop through, say, 10 elements -- but closer to millions, none of the iterations of which actually load anything from fin:
for(m = 0;m < numberofpeopleingroup;m++)


This kind of mistake is why I tend to prefer errors be reported using exceptions rather than flags. Before actually using fin and fout, check if fin.is_open() && fout.is_open().


Thank you very much I never guessed that it can be a problem like that I'll add a check method soon.

Share this post


Link to post
Share on other sites
YaY ! I found the problem I was "fin"ing and checking if (fin != "") to find the end of the file. But I'm now using file.eof() !

But I've a further problem, When I give the input on my computer I get the outup right but when I upload it there, It gives me slightly different output.Can it be some kind of buffer problem of fin :S or something like that? anyone?

Here is the slight difference;

Here are the respective outputs:
----- our output ---------
mitnik_2923
Poulsen_557
Tanner_128
Stallman_-311
Ritchie_-1777
Baran_245
Spafford_-1997
Farmer_440
Venema_391
Linus_-599
---- your output ---------
mitnik_3923
Poulsen_557
Tanner_128
Stallman_-311
Ritchie_-1777
Baran_245
Spafford_-1997
Farmer_440
Venema_391
Linus_-1599
--------------------------



But in my computer it gives the exact same "true" output.

[Edited by - Eralp on January 1, 2008 5:56:02 AM]

Share this post


Link to post
Share on other sites
In your code, you have 9 variables which are declared without being initialized. I'm not saying that they will be used before they are initialized, but you should consider this possibility. It's bad practice to do this, you should instead declare your variables and initialize them at the same time in all but the most extreme circumstances.

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
In your code, you have 9 variables which are declared without being initialized. I'm not saying that they will be used before they are initialized, but you should consider this possibility. It's bad practice to do this, you should instead declare your variables and initialize them at the same time in all but the most extreme circumstances.


Yes I learned that as I was trying to find something :) thank you for helping me.Also I finished that project and the next one, so this thread is kinda SOLVED.

It was all the fault of "end of the file" thing :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this