![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
Hi!
I'm a newbie at programming c. I'm currently working thru the steps of a introduction to c, as preparation for future assignments for a neural networks course. Unfortantly the steps in the introduction aren't really suited for someone who's new to the programming language I'm trying to write a program that reads in two vectors/arrays and computes their dot product. Most is working, but I have no idea how to create a datafile like the following: So, generate a file (with a header and with data) something like: 5 0.3 4.5 1.9 2.3 3.3 1.178 3.2 4.5 0.19 99.99 First of, what's a header? Is it the #include <stdio.h> stuff, should I put that at the beginning of the data file? Or does it mean i should make some kind of .h file for libaries (read something like that in a tutorial on the web)? And can I simple copy paste this data into the file? So far I have the following program to read the datafile: #include <stdio.h> #include <stdlib.h> #include <math.h> int main (int argc, char *argv[]) { FILE *fp; int n; if (argc!=2) { fprintf(stderr,"use: inprod 'fname'!\n"); exit(1); } if ((fp=fopen(argv[1],"r"))==NULL) { fprintf(stderr,"unable to open '%s' for reading!\n",argv[1]); exit(1); } fp = fopen(argv[1], "r"); fscanf(fp,"%d",&n); printf("%d",n); /* and here you should loop over n to read the x[i] and w[i] */ } The printf is meant to check what it's reading and shoul produce the number 5, but instead I get 1108551892, which I guess is some kind of memory location(?). Can anybody here please explain to me (in baby steps :o ) what I need to do or am doing wrong? I'll be eternally gratefull! For completeness sake, below the computing part of my programm: (BTW I have a sneaking suspicion that it won't work on the datafile in it's current format, even if I can read it in) #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXLENGTH 16 double w[MAXLENGTH], x[MAXLENGTH], y[MAXLENGTH]; int main (int argc, char *argv[]) { int n; int i; double z; double total = 0; printf("Welcome to my first C-program!"); printf("please type the length of the vector 'w':"); fflush(stdout); for (i = 0; i < MAXLENGTH; i++) scanf ("%lf",&w[i]); for (i = 0; i < MAXLENGTH; i++) printf("w [%d] = %lf \n", i, w[i]); printf("please type the length of the vector 'x':"); fflush(stdout); for (i = 0; i < MAXLENGTH; i++) scanf ("%lf",&x[i]); for (i = 0; i < MAXLENGTH; i++) printf("x [%d] = %lf \n", i, x[i]); /*maakt een lijst y met de producten van de elementen uit lijst w en x */ for (i=0; i<MAXLENGTH; i++) y[i] = w[i] * x[i]; for (i=0; i<MAXLENGTH; i++) printf ("y[%d] = %lf\n",i, y[i]); /*Telt de elementen uit lijst y bij elkaar op */ for (i=0; i<MAXLENGTH; i++) total += y[i]; printf ("het inproduct is %lf\n",total); return 0; } |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2005
Posts: 4
Rep Power: 0
![]() |
I solved it!
Yeeh, I solved it myself! Am over the moon right now....what a good feeling
This is my final, working code: #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXLENGTH 16
double w[MAXLENGTH], x[MAXLENGTH], y[MAXLENGTH];
int main (int argc, char *argv[])
{
FILE *fp;
int n;
int i;
double total = 0;
if (argc!=2) {
fprintf(stderr,"use: inprod 'fname'!\n");
exit(1);
}
if ((fp=fopen(argv[1],"r"))==NULL) {
fprintf(stderr,"unable to open '%s' for reading!\n",argv[1]);
exit(1);
}
fp = fopen(argv[1], "r");
fscanf(fp,"%d",&n);
/* and here you should loop over n to read the x[i] and w[i] */
for (i = 0; i < n; i++)
fscanf (fp,"%lf",&w[i]);
for (i = 0; i < n; i++)
fscanf (fp,"%lf",&x[i]);
for (i = 0; i < n; i++)
printf("w [%d] = %lf \n", i, w[i]);
for (i = 0; i < n; i++)
printf("x [%d] = %lf \n", i, x[i]);
/*makes a list y containing the products of the elements from list w and x*/
for (i=0; i<n; i++)
y[i] = w[i] * x[i];
for (i=0; i<n; i++)
printf ("y[%d] = %lf\n",i, y[i]);
/*adds the elements from list y*/
for (i=0; i<n; i++)
total += y[i];
printf ("het inproduct is %lf\n",total);
return 0;
}5 0.3 1.9 3.3 3.2 0.19 4.5 2.3 1.178 4.5 99.99 Probably not the most compact code to do this, but I'm just so happy it works! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|