|
|
#1 |
|
Why am I multiplying when I want to divide (C)?
Here's the code:
Code:
p = 0;
for ( i = 0; i < lp; i++)
{
p += fwrite(&Pt[i*pagesize], sizeof(char), pagesize, fp);
}
fclose(fpt);
printf("%d\n", p);
printf("%d %d \n",pagesize, p/pagesize);
264600 5400 14288400 Does this make sense to anybody???? |
|
|
|
0
|
|
|
#2 |
|
What are the types of p and pagesize?
Code:
#include <stdio.h>
int main (int argc, const char * argv[])
{
size_t p;
size_t pagesize;
p = 264600;
pagesize = 5400;
printf("%d\n", p);
printf("%d %d \n",pagesize, p/pagesize);
printf("%d * %d = %d\n", p, pagesize, p*pagesize);
}
Code:
264600 5400 49 264600 * 5400 = 1428840000 Last edited by chown33; Feb 5, 2010 at 05:46 PM. Reason: change types to size_t |
|
|
|
0
|
|
|
#3 |
|
The question seems about as malformed as the code fragment.
Looks to be writing an 'lp' count of 'pagesize' blocks of the array 'Pt' to the stream 'fp'. The multiplication is used to calculate an 'offset' within the array 'Pt'. |
|
|
|
0
|
|
|
#4 |
|
Sorry, I don't understand. I've never declared a variable using size_t. Besides, I don't see any likelihood of overflow.
One of us is missing something. I am writing lp pages sized pagesize to a file. p should be pagesize*lp but this is fact has little bearing on the two print statements that follow. If p is 264600 and pagesize in 5400, why is 264600/5400 not 49 as in chown's code. Last edited by kainjow; Feb 5, 2010 at 08:01 PM. Reason: merged posts |
|
|
|
0
|
|
|
#5 |
|
Code:
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); Further hint: Code:
int fwrite( const void *buffer, size_t size, size_t count, FILE *stream ); Last edited by kainjow; Feb 5, 2010 at 07:54 PM. Reason: merged posts |
|
|
|
0
|
|
|
#6 |
|
You trying to out terse me.
Modifying the code doesn't change the output. fwrite returns the number of bytes read, it was read in in chunks of pagesize. a total of p/pagesize should print 49. Code:
fpt = fopen(filename, "w");
p = 0;
for ( i = 0; i < lp; i++)
{
p += fwrite(&Ptrading[i*pagesize], sizeof(char), (size_t) pagesize, fpt);
}
fclose(fpt);
printf("%d\n", p);
printf("%ld %ld \n",(size_t)pagesize, (size_t)p/(size_t)pagesize);
pagesize is #define to be 5400. Pt is an array allocated to be much larger than lp*pagesize Last edited by kainjow; Feb 5, 2010 at 07:54 PM. Reason: merged posts |
|
|
|
0
|
|
|
#7 | |
|
Quote:
You don't have to provide real data. You can use artificial data, generated in any manner (including not generated at all, in which case the output data will be all zeros). All the data types, sizes, etc. should be identical to your real program. You should allocate memory exactly like your real program. You should write data (which was artificially generated) in the same manner as the real program does. Not doing so would defeat the point of the program. You should have a main() function and whatever other functions you think make sense in a small self-contained program. If the self-contained program doesn't exhibit the problem, then you have to figure out why that is. If the self-contained program does exhibit the problem, then everyone else will be able to see it, and some may even be able to determine why. As it stands now, you're the only one who has actual compilable code, hence the only one who has actual debuggable code. However, you also find yourself unable to debug that code. The only way to solve the problem is to create the intersection of compilable code with debugging skills. If that intersection remains empty, then all you can do is solve the problem by accident. |
||
|
|
0
|
|
|
#8 |
|
No not at all. I'm simply trying to give you and idea of what it's like when the other end of a conversation expects you to be a mindreader and work with assumed common information where there is none.
I was going to post something to the effect of what chown33 just posted so I'll let his stand. |
|
|
|
0
|
|
|
#9 |
|
The fact that p came out as 264600 when expressed as an integer does not necessarily mean it is an integer and will follow integer division.
Try printf("%d %d \n",pagesize, (int)p/pagesize); or maybe printf("%d %d \n",pagesize, (int)p/(int)pagesize); As an idea... EDIT: my first inclination would be to also set a breakpoint down in there somewhere and observe the values. |
|
|
|
0
|
|
|
#10 |
|
guys.
On (too) many occasions, I have suffered because I have not posted the full code. You would easily see things I have missed. On the other hand, your responses, when not yelling at me, are usually helpful; I eventually get it. So let me just be a little more specific since my working skills preclude writing other test code until absolutely necessary. I'm an old dog stuck in my ways.
The file gets written to; I've looked at it and it seems ok. The division error seems to be out of left field. Do you a clue? Any suggestions where I should look. thanks |
|
|
|
0
|
|
|
#11 |
|
He gets it but won't follow the path to enlightenment.
I'm 53 but I'm still learning and applying it as well. |
|
|
|
0
|
|
|
#12 |
|
my god, after reading this thread, I'm glad I don't write code for a living...
__________________
K. |
|
|
|
0
|
|
|
#13 |
|
Thanks Jared you almost got it.
This works; don't know why but it does. int s; s = pagesize; printf("%d\n", p); printf("%d %d \n",pagesize, (p/s)); Lloyd, See a few ideas and I got it. I'm 62, a New Yorker and very stubborn. Here's one project you guys probably helped on. http://tierneylab.blogs.nytimes.com/...q=Alcor&st=cse Last edited by kainjow; Feb 14, 2010 at 09:22 PM. Reason: merged posts |
|
|
|
0
|
|
|
#14 |
|
You get enough 'novice' mind readers together and eventually someone is going to have enough information to combine with their interpretation of your question in order to give you a 'useful' answer.
The fact is things could go quicker and you'd get more relevant response if you provided more complete questions and useful information with which to work. I'm trying to be helpful but nothings happening ... |
|
|
|
0
|
|
|
#15 |
|
What platform are you compiling for?
Its like its putting 5400 into an odd size or something at compile time. Thus its not obeying normal arithmetic. or 264600 is getting converted to whatever type the compiler chose for 5400 and it gives weird results.... |
|
|
|
0
|
|
|
#16 |
|
Lloyd
Xcode on Snow Leopard.
|
|
|
|
0
|
|
|
#17 | |
|
Quote:
|
||
|
|
0
|
|
|
#18 |
|
|
0
|
|
|
#19 |
|
Jared
Yes, very odd. Haven't got a clue. pagesize is defined as
#define rows 10 #define cols 45 #define element 12 #define pagesize rows*cols*element |
|
|
|
0
|
|
|
#20 | |
|
Quote:
Do a textual replacement on your original expression: Code:
p/pagesize Code:
p/rows*cols*element Redo your define as: Code:
#define pagesize (rows*cols*element) |
||
|
|
0
|
|
|
#21 | |
|
Quote:
p/pagesize = p/10*45*12= 14288400 Its funny because I kept trying to figure out what was changing p or pagesize by a factor of 100. Well you took a factor of 10 out of pagesize and devided p by that factor of 10. |
||
|
|
0
|
|
|
#22 |
|
thanks. eom.
|
|
|
|
0
|
|
|
#23 |
|
farmerdoug,
Remember this, or did you ignore it, from the last thread. Code:
#define kPAGES (2000)
#define kROWS (10)
#define kCOLUMNS (45)
#define kBYTE_COUNT_PER_COLUMN (12 * sizeof(char))
#define kBYTE_COUNT_PER_ROW (kCOLUMNS * kBYTE_COUNT_PER_COLUMN)
#define kBYTE_COUNT_PER_PAGE (kROWS * kBYTE_COUNT_PER_ROW)
char* allocate_memory(void)
{
void* ptr = calloc((kPAGES * (kROWS * kCOLUMNS)), kBYTE_COUNT_PER_COLUMN);
if ( NULL == ptr )
{
printf("no memory");
}
return (char*)ptr;
}
//n = number of items to sort
void sort(char* array, int n, int row, int column)
{
char tmp[kBYTE_COUNT_PER_PAGE];
#define INDEX(PP,RR,CC) ((PP * kBYTE_COUNT_PER_PAGE) + ((RR * kBYTE_COUNT_PER_ROW) + (CC * kBYTE_COUNT_PER_COLUMN)))
#define DUMP(NN) for(int i=0; i<NN; i++) {printf("%s \n ",&array[INDEX(i,0,3)]);}
DUMP(n);
printf("\n");
for ( int j = (n - 1); j >= 0; j-- )
{
for ( int k = 1; k <= j; k++ )
{
if ( atoi(array + (k - 1)*kPAGES + row*kROWS + column*kCOLUMNS) > atoi(array + (k)*kPAGES + row*kROWS + column*kCOLUMNS) )
{
printf("\n");
memcpy(tmp, &array[INDEX(k - 1, 0, 0)], kBYTE_COUNT_PER_PAGE);
memcpy(&array[INDEX(k - 1, 0, 0)], &array[INDEX(k,0,0)], kBYTE_COUNT_PER_PAGE);
memcpy(&array[INDEX(k,0,0)], tmp, kBYTE_COUNT_PER_PAGE);
DUMP(n);
}
}
}
#undef INDEX
#undef DUMP
}
Again, please, please, PLEASE given complete definitions of ALL variables and macros in ANY and ALL code posts for the quickest most useful responses. Anyway have and nice weekend. EDIT: This hasn't been compiled and was done off the top me me head and may contain syntax errors Last edited by lloyddean; Feb 5, 2010 at 09:50 PM. |
|
|
|
0
|
|
|
#24 |
|
Lloyd
No, I didn't remember it. Thanks for the help. Can't have a good weekend until this code is done LOL. Best.
|
|
|
|
0
|
|
|
#25 |
|
Always use parens, even when you think they might not be needed. Consider this little error I made not too long ago:
Code:
int eventKeys[[NSApp currentEvent] modifierFlags];
if ( eventKeys&NSShiftKeyMask != 0 ) {
// extend the selection
}
__________________
Mr. Paul, sir, I thought you should be advised, there seems to be a zombie tribble clinging to your head, for it is scarfing your brain
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| Before I die I want to ___________. | MarkMS | Community Discussion | 30 | Apr 7, 2011 06:59 PM |
| I am an ATT Customer and Want to PAID FULL PRICE, NOT POSSIBLE??? | sevimli | iPhone | 5 | Jun 25, 2010 09:26 AM |
| Am I missing something? I want to put my son's DVD movies on my old iPod. | Scottsdale | iPod | 15 | Oct 28, 2008 09:04 AM |
| why am I printing from last page to first | fjs08 | Mac OS X | 15 | Nov 6, 2006 05:48 AM |
| Some photos I am really proud of (really wanted to share) | potatoarms | Picture Gallery | 16 | Feb 8, 2006 01:37 PM |
All times are GMT -5. The time now is 01:06 PM.








Linear Mode

