473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

reading from file and storing in array for Linear Regression (C)

1
Hi There,

I am learning C for university and I am stuck on a question.

I am trying to achieve the following.

1. Read Data from file - This works

Expand|Select|Wrap|Line Numbers
  1. int readfile()
  2. {
  3.     FILE *fptr;
  4.     char filename[15];
  5.     char ch;
  6.  
  7.  
  8.     printf("Enter the filename to be opened \n");
  9.     scanf("%s", filename);
  10.     /*  open the file for reading */
  11.     fptr = fopen(filename, "r");
  12.     if (fptr == NULL)
  13.     {
  14.         printf("Cannot open file \n");
  15.         exit(0);
  16.     }
  17.     ch = fgetc(fptr);
  18.     while (ch != EOF)
  19.     {
  20.        ch = fgetc(fptr);
  21.     }
  22.     fclose(fptr);
  23. }
2. Store data read from file into two arrays (this data is comma delimited).

3. Perform mathematical calculation on the values. - this is done, albeit manual entry at the moment.



The data is stored as per below:

x,y
x1,y1
x2,y2
...
xn,yn

The data also varies in size (so the file could be 30 lines of x and y co-ordinates, or 100).


Any pointers would help.

Thanks
Jul 2 '19 #1
1 1661
dev7060
636 Expert 512MB
fscanf() seems a good option to use here. You can use it like:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. while(fscanf(fptr, "%f,%f", &var1, &var2)==1){
  3.   //Perform calculations
  4. }
  5. ...
  6.  
Jul 2 '19 #2

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

Similar topics

14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
5
by: James | last post by:
i've a text file in the following format and i would like to read into this array text file ====== machine1 machine2 Dim sr As StreamReader = New StreamReader("machines.txt") dim line as...
15
by: nikie | last post by:
I'm a little bit stuck with NumPy here, and neither the docs nor trial&error seems to lead me anywhere: I've got a set of data points (x/y-coordinates) and want to fit a straight line through...
3
by: sql guy123 | last post by:
This is a real challenge. I hope someone is smart enough to know how to do this. I have a table TABLE1
1
by: wirecom | last post by:
Hi all, I am seeking a module that will do the equivalent of linear regression in 3D to yield a best fit a plane through a set of points (X1, Y1, Z1), (X1, Y1, Z1),... (Xn, Yn, Zn). The...
6
by: chris237 | last post by:
I'm having trouble with the following program. i want it to have the options to save a list of data entered by the user and display it the next time they run it. Could someone please either show me a...
1
by: Nod Lee | last post by:
Hi all Is there any tools/objects from Microsoft that I can use to perform linear regression in webform and generate the results into a graph? If not, is there any recommendations for 3rd...
3
by: ntuyen01 | last post by:
Hi All, Does anyone has examples for Linear Regression formula using c#. Or any good third party software create this. Thanks in advance Regards, Ted
7
by: PlsHelp | last post by:
Hi every1,this is my 1st time using this forum....pls can any1 tell me how to create a code in VB that can do LINEAR REGRESSION in excel.....i'm not sure how to xplain exactly what i need but...
0
by: drfish | last post by:
Hi, I'm a complete novice when it comes to VB so need some help. I would like a macro that performs linear regression for different cell ranges each time it is run, depending on the number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.