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 - #include <stdio.h>
-
#include <math.h>
-
int main (void)
-
#define FILENAME "data_21.txt"
-
#define CRT_SECURE_NO_DEPRECATE
-
{
-
double base;
-
int p, t, count, q, m, k;
-
FILE *data_2;
-
-
printf("enter an odd number as base");
-
scanf_s("%lf", &base);
-
data_2=fopen("data.txt","w");
-
-
-
-
for (p=9;p<=15;p++)
-
{
-
base=base*p;
-
for(k=1;k<=8;k++)
-
{
-
m=base*k;
-
fprintf(data_2"%20d",m);
-
}
-
-
printf("\n");
-
}
-
data_2=fopen("data.txt","r");
-
fscanf(data_2,"%lf",&m);
-
-
return 0;
-
}
5 1831 -
{
-
double base;
-
int p, t, count, q, m, k;
-
-
printf("enter an odd number as base");
-
scanf_s("%lf", &base);
-
-
for (p=9;p<=15;p++)
-
{
-
base=base*p;
-
for(k=1;k<=8;k++)
-
{
-
m=base*k;
-
fprintf(data_2"%20d",m);
-
}
-
-
printf("\n");
-
}
-
}
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. -
int Multiple = 0;
-
for(p = 1; p<=48; p++)
-
{
-
Multiple = base*p;
-
fprintf(data_2"%20d",Multiple);
-
///Here we check to see if we need a new line
-
if(p % 8 == 0)
-
printf("\n"); //print new line
-
}
-
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!
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
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
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. -
int Multiple = 0;
-
for(p = 1; p<=48; p++)
-
{
-
Multiple = base*p;
-
fprintf(data_2"%20d",Multiple);
-
///Here we check to see if we need a new line
-
if(p % 8 == 0)
-
printf("\n"); //print new line
-
}
-
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!!!!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J. W. McCall |
last post by:
Sorry if this is OT, but here's my question:
I wrote a simple python script to increment a counter in a text file,
and I wanted this script to be accessed whenever an HTML file is
accessed. The...
|
by: dhnriverside |
last post by:
Hi peeps
Ok, so I thought I'd have a go at making a console app in VS2k5... I haven't
written any windows apps for years, let alone dos apps (been web programming)
and I've hit a dumb error...
...
|
by: Kentor |
last post by:
hello, im trying to make a little loop, but i cant figure it out... i
have a string with a bunch of 1s and 0s in it: 110101010101111010101
.... i need to count the number of 1s divide it by 2 and...
|
by: g35rider |
last post by:
Hi, I have the following code that is giving this error, I cant
simplify the code, I was just testing some theory for something we are
doing and was getting an issue here. Please someone point out...
|
by: partybob99 |
last post by:
I dont know why this is so difficult for me, but I've looked literally
everywhere on how to create a embedded crystal report in ASP.NET and
then access it using the crystal report viewer.
Here...
|
by: rdufour |
last post by:
Downloaded a sample that is supposed to show how to localize a web site and
started playing with the code, its in Vs2003, so I figure I would take code
and put it in Vs 2005 and doing so use it a...
|
by: stephenrussett |
last post by:
Ok i what i am trying to do is:
I have a query that pumps out information like this:
(1) (2) (3) (4) (5)
1 1 Cooper Street NS...
|
by: WolfgangS |
last post by:
Ok first off, i am a total beginner at this groups stuff and i have no
clue how this works. This is probabaly the wrong group for my problem
but i will post it anyways. Learning by doing right?
...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |