By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,611 Members | 1,635 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,611 IT Pros & Developers. It's quick & easy.

I know this is a dumb question but I cant figure this out

P: 12
I am trying to do my assignment but I cant figure out how to get 8 data points per line to print to a file.
Here is the assignment

1. Program asks the user to enter an odd number as a BASE, using this Base it creates 48 multiples of the BASE and prints in a data file "data_3b.txt", 8-data per line.

2. Program opens this data file, scans the contents of "data_3b.txt" file and counts all the odd data, and prints them on the screen: 4 data per line.

3. Program computes the average of all the odd data. It prints the number of odd data points and their average on the screen in the given FORMAT.


I havent even gotten to reading the file, which I dont think I can do anyway. I'm still trying to figure out how to get 8 data points on one line, LOL! Here is my program so far. Sorry its not right justified. Can someone please help me. Also how am I supposed to tell the program to stop reading whenever I get to that part!
thanks for any help

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main (void)
  4. #define FILENAME "data_21.txt"
  5. #define CRT_SECURE_NO_DEPRECATE
  6. {
  7.     double base;
  8.     int p, t, count, q, m, k;
  9.     FILE *data_2;
  10.  
  11.     printf("enter an odd number as base");
  12.     scanf_s("%lf", &base);
  13.     data_2=fopen("data.txt","w");
  14.  
  15.  
  16.  
  17.     for (p=9;p<=15;p++)
  18.     {
  19.         base=base*p;
  20.         for(k=1;k<=8;k++)
  21.         {
  22.             m=base*k;
  23.             fprintf(data_2"%20d",m);
  24.         }
  25.  
  26.         printf("\n");
  27.     }
  28. data_2=fopen("data.txt","r");
  29. fscanf(data_2,"%lf",&m);
  30.  
  31. return 0;
  32. }
Feb 19 '08 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 464
Expand|Select|Wrap|Line Numbers
  1. {
  2.     double base;
  3.     int p, t, count, q, m, k;
  4.  
  5.     printf("enter an odd number as base");
  6.     scanf_s("%lf", &base);
  7.  
  8.     for (p=9;p<=15;p++)
  9.     {
  10.         base=base*p;
  11.         for(k=1;k<=8;k++)
  12.         {
  13.             m=base*k;
  14.             fprintf(data_2"%20d",m);
  15.         }
  16.  
  17.         printf("\n");
  18.     }
  19. }
First why do you have so many variables for the first part? Variables named 'p' ,'q', 'm' ect. are bad names for variables so try to make them more descriptive. In your loops why do you have p= 9? <=15? what logic is behind that? Seeing how you need to print out 48 multiples i would use that as check condition for your loop. Then i would check to see if we have hit 8 items in a line. If so \n if not do nothing.

Expand|Select|Wrap|Line Numbers
  1. int Multiple = 0;
  2. for(p = 1; p<=48; p++)
  3. {
  4.      Multiple = base*p;
  5.      fprintf(data_2"%20d",Multiple);
  6. ///Here we check to see if we need a new line
  7. if(p % 8 == 0)
  8.      printf("\n"); //print new line
  9. }
  10.  
Feb 20 '08 #2

P: 12
thanks for the reply. I know I have alot of variables defined, I was just randomly trying things so I had to create alot of variables. Not a good idea I suppose. I did realize that I can use a loop imbedded in another loop to get it to print 8 characters per line, but I cannot figure out how to print 4 characters per line to the screen. I could do the modulus of 2 and print, but I have no clue how to have it print 4 characters, it keeps printing one!
Feb 20 '08 #3

Expert 100+
P: 464
post your code and we will be glad to help (specifically the part on how you get the data from the file and then the part of where you try and display it on the screen). how is the data in the file separated? Seeing how it says print 4 data items per line each data item could vary in length correct (EX: Data value 1 = 333 Data value 2 = 3579)? so you will probably need to print more than just 4 characters to the screen.

Logically heres how i see it.

Grab Data Item
Is Data Item Odd?
no-> continue
yes-> Print Data Item on screen
Was the last printed item the fourth data item for that line?
yes--> New Line
no --> continue
Feb 20 '08 #4

P: 12
here is my loop for the 8 data points per line calculation, this works beautifully, but I cant seem to grasp how to do it again for the 4 data points per line
for (p=1;p<=6;p++)
{
m=(base*p);
for(k=9;k<=16;k++)
{
fprintf(data_2,"%5d",(m*k));
fscanf("%lf",(m*k));

HERE this is what I have tried, but I dont really know what to count. Technically I know how many odd data points there are, but how do I tell the program to count them and print them 4 per line, because I dont know how high count should count
if ((m*k)%2!=0)
for (count=0;count<=48;count++)
printf("%7d\n",(m*k));

}

}
return 0;
}

Thanks so much for your response
Feb 20 '08 #5

P: 12
First why do you have so many variables for the first part? Variables named 'p' ,'q', 'm' ect. are bad names for variables so try to make them more descriptive. In your loops why do you have p= 9? <=15? what logic is behind that? Seeing how you need to print out 48 multiples i would use that as check condition for your loop. Then i would check to see if we have hit 8 items in a line. If so \n if not do nothing.

Expand|Select|Wrap|Line Numbers
  1. int Multiple = 0;
  2. for(p = 1; p<=48; p++)
  3. {
  4.      Multiple = base*p;
  5.      fprintf(data_2"%20d",Multiple);
  6. ///Here we check to see if we need a new line
  7. if(p % 8 == 0)
  8.      printf("\n"); //print new line
  9. }
  10.  
OMG!!!!!!!!!!!!! I just reread your post and that is the answer! Thank you so much, its so simple, to use the modulus to create a new line!!!!!

I LOVE YOU!!!!
Feb 20 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.