hi
i need help with listing sales only for 2009 in various data inputs.
how do i do that?
so i need to search the data and print out only the data in 2009.!!
help needed!!!!!!!!
thnx
hi
i need help with listing sales only for 2009 in various data inputs.
how do i do that?
so i need to search the data and print out only the data in 2009.!!
help needed!!!!!!!!
thnx
The answer depends on how the sales data is organized. Usually, if you write your program to mimic how you would do it, by hand, you have a good starting place.
Generally, one column of the data would be the year's date, and making a struct for the data, and then making an array of those structs, provides a big advantage for programs like this.
Give it a try, and post up what you have tried, if you get stuck.
hi
ok.....this is what i have so far:
//Stage 2: printing sales that happened in year 2009
printf("\n-----------------------------------------------\nStage2\n-----------------------------------------------\n");
printf("Listing of all the sales recorded for the year 2009:\n");
printf("+========================================= +\n| Postcodes Month Year Bedrooms Price |\n+-----------------------------------------+\n");
int i;
for(i=0; Year[i]<2009; i++) {
printf("| %4ld %02d %4ld %d %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
ok.....so what i am supposed to do is scan the input data(which can be like 10 000 lines long) and print out for the year 2009 how many sales took place etc...
how do i do that by using arrays?
thank you
Please post your code in code tags next time.
It is much better to ask about things you are having trouble with then asking how to do such broad and general things. You probably want to start reading about structures (they're fairly simple) and once you can read data for one sale into a structure, try making an array of them.
Once you know how to use an array of structures, and you can read data into one structure, you should be able to complete your task easily.
(If you just want to use several arrays for each elements it's also do-able, but much messier.)
hi
ok...thnx for that.....
we have not been taught structures so i dont think we can use them....
also this is what i have
and in the sample10.txt: the last line is printed out instead of actually printing the sales only for 2009.Code:int read_num(int [2009], int *num);{ int num; getchar(); while(ch!='\n') scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch); while(ch!='\n') while( scanf("%d", &num) ==1); { if (num<=2009) { } printf("| %4ld %02d %4ld %d %7ld |\n", Postcodes, Month, Year, Bedrooms, Price); } }
any ideas?
i dont think my program scans the whole data and reads the 2009 sales only..
thank you so much for ur help.
If that code compiles then your compiler is broken.
If you have not yet learnt how to write functions, it's probably best to stay away from them, at least until you have finished this particular program. You seem to be biting off more than you can chew. Of course, I'm assuming this homework and it is due sometime, if it's just for learning purposes then knock yourself out, you might find it difficult though.
Do you need to hold the data in memory or just print it out?
actually i just need the print out of it.
so i am suppose to scan the file and print out the data for only the sales that happened in year 2009.
i hope that makes it clearer/
thnks
Ok, I think I see what you're trying to do now.
Depending on the format of the data for a particular sale, you can just read in all the data for one sale, then test whether the year is equal to 2009 and if so, print it. You've sort of got the right idea but your execution is a little off. :P
for some pseudocode
This method only requires some basic knowledge of loops and printf/scanf.Code:while there is still input read the data on the line into the respective variables if year == 2009 print the variables repeat
ok...so i tried to do that aswell.....
however....it still doesnt work
this is what i have:
so can i use arrays for postcodes only?Code:getchar(); while(ch!='\n') scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch); while(ch!='\n') while( scanf("%ld %d %ld %d %ld\n", &Postcodes, &Month, &Year, &Bedrooms, &Price)!=EOF);{ if (Year==2009){ } } printf("| %4ld %02d %4ld %d %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
and then tell it to scan etc....
thnx
I'm not sure what the point of this code here is? It's just reading five characters the last of which will be in ch.Code:getchar(); while(ch!='\n') scanf("%c, %c, %c, %c, %c", &ch,&ch,&ch,&ch,&ch);
You seem you have repeated 'while(ch!='\n')' for some reason. When scanf reads the data in, it will discard whitespace (depending on the format specifiers, but yours are fine). Therefore you only need to test for when there is no more input. If the lines of data are on different lines scanf will take care of that for you, as long as there that data is always there.Code:while(ch!='\n') while( scanf("%ld %d %ld %d %ld\n", &Postcodes, &Month, &Year, &Bedrooms, &Price)!=EOF);{ if (Year==2009){ } } printf("| %4ld %02d %4ld %d %7ld |\n", Postcodes, Month, Year, Bedrooms, Price);
Make sure you have your format specifier in scanf correct, this is the format that the data will be in.
means that there will be a long integer, then 5 spaces then an integer then 2 spaces so on. If you describe the way that the data is formatted then we can tell if it's correct or not.Code:"%ld %d %ld %d %ld\n"
Your while loop isn't actually executing the if statement, even if it returns true. You have place a semi colon at the end of the while loop.
Also, your print statement isn't inside your if statement, so if the loop executes it will print regardless of whether the year is 2009 or not.
Last edited by DeadPlanet; 04-18-2010 at 02:51 AM.
hi
ok...thank u very much for that...
also i want to store the input data(by stdin) in to 2 d arrays
can u plz tell me how to do that?
this is what i have so far:
thnx in advancedCode:#define ROWS 5 #DEFINE COLUMNS 10000 //#define MAXVALS 10000 //int read_int_array(int[], int); //void sort_int_array(int[], int); int main(int argc, char *argv[]) { //declaration & initialisation long Postcodes=0; int Month=0; long Year=0; int Bedrooms=0; long Price=0; char ch=0;; int array2d[ROWS][COLUMS] { data[n][0] = Postcodes; data[n][1] = Month; data[n][2] = Year; data[n][3] = Bedrooms; data[n][4] = Price; n++; } }
Papermate, do you need to re-organize the 2009 data lines, once you find them OR do you just need to print every line (row) of data, for 2009, exactly as it is?
Because if you just have to find and print out those 2009 lines of data, then there's a very easy way to do this -- way easier than what you're trying to do now.
And I'm all over easy, don'cha knooow!
ok...we need to examine the input data from stdin
so no i dont have to re-organise the 2009 data lines just print out every row...
so can u please tell me the easy way?
thanks
well.. yeh no one will do that
however....when they runs my program they will do this:
./filename <sample10000.txt
so its the same as typing it in
ok. so can u plz tell me the code now?