Hey,
This is our first assignment with arrays, and I need to make an array that can read up to 100 items from a file (even though the file only has 31 numbers).
Not quite sure what to do.
Printable View
Hey,
This is our first assignment with arrays, and I need to make an array that can read up to 100 items from a file (even though the file only has 31 numbers).
Not quite sure what to do.
What are "items"? If they're all of the same type, like integers, you can declare the array like so:
Then just read the file like you normally do and assign to each index in the array:Code:int file[100];
Code:/* Gather numbers from the file */
for ( i = 0; i < 100; i++ ) {
if ( fscanf ( in, "%d", &file[i] ) != 1 )
break;
}
/* Display gathered numbers */
for ( j = 0; j < i; j++ )
printf ( "%d\n", file[j] );
what does this "break" do?
It ends the loop prematurely.
Or, rather, if the return value of fscanf is not one, the loop ends. (fscanf returns the number of items successfully read.)
when you are displaying the gathered numbers with array a[j], when i delclare array 'a' at the start, what is its value
>when i delclare array 'a' at the start, what is its value
a doesn't exist. I usually call my scratch arrays a, and habit forced me to introduce a typo. It should be file, not a. The code has been fixed.
This is what I've got....But for some reason it just returns 0's, not the numbers in the file.
Code:#include <stdio.h>
#include <math.h>
int file[100];
int main (void)
{
int i, j;
FILE *inp;
inp = fopen("temperature.txt", "r");
/* Gather numbers from the file */
for (i = 0; i < 100; i++)
{
fscanf (inp, "%d", &file[i]);
}
/* Display gathered numbers */
for (j = 0; j < i; j++)
{
printf ("%d\n", file[j]);
}
fclose(inp);
return (0);
}
Looks fine to me. (aside from missing break; and error checking) also no reason to include math.h, maybe show us your input file?
You must error check fopen() and fscanf().
See posts above for error checking fscanf().Code:inp = fopen("junk1.txt", "r");
if (inp == NULL)
{
perror("fopen");
...
}
it looks like my program cant find the file. where on the hard drive does it have to be? it seems like ive tried to put it everywhere imaginable.
with no path (as you have it) you would need to place it inside the the same directory as the program itself.
This is why you have to check the return of fopen (to see if it actually openned anything :)
Here's what I have so far. My file temperature.txt is in the right place and everything.
Code:#include <stdio.h>
#include <math.h>
#define SIZE 100
int temp[SIZE];
int main (void)
{
int i, sum, sum_sqr;
double mean, st_dev;
FILE *inp;
inp = fopen("C:\\Program Files\\Miracle C\\temperature.txt", "r");
if (inp == NULL)
{
printf("ERROR OPENING THAT FILE\n");
}
/* Gather and prints all the data from the file */
for (i = 0; i < SIZE; i++)
{
fscanf(inp, "%d", &temp[i]);
printf("%d\n", temp[i]);
}
/*Computes the sum and the sum of the squares of all the data*/
sum = 0;
sum_sqr = 0;
for (i = 0; i < SIZE; i++)
{
sum+=temp[i];
sum_sqr += temp[i] * temp[i];
}
/*Computes and prints the mean and standard deviation*/
mean = sum / SIZE;
st_dev = sqrt(sum_sqr / SIZE - mean * mean);
printf("The mean is %.2f.\n", mean);
printf("The standard deviation is %.2f.\n", st_dev);
fclose(inp);
return (0);
}
there u go the working code.
ssharish2005Code:#include <stdio.h>
#include <math.h>
#define SIZE 100
int temp[SIZE];
int main (void)
{
int i, sum, sum_sqr,tempi;
double mean, st_dev;
FILE *inp;
inp = fopen("C:\\Program Files\\Miracle C\\temperature.txt", "r");
if (inp == NULL)
{
printf("ERROR OPENING THAT FILE\n");
}
/* Gather and prints all the data from the file */
for (i = 0; i<SIZE; i++)
{
if(fscanf(inp, "%d", &temp[i])!=1)
break;
printf("%d\n", temp[i]);
}
/*Computes the sum and the sum of the squares of all the data*/
sum = 0;
sum_sqr = 0;
tempi=i;
for (i = 0; i < tempi; i++)
{
sum+=temp[i];
sum_sqr += temp[i] * temp[i];
}
/*Computes and prints the mean and standard deviation*/
mean = (double)sum / SIZE;
st_dev = sqrt(sum_sqr / SIZE - mean * mean);
printf("The mean is %.2f\n", mean);
printf("The standard deviation is %.2f.\n", st_dev);
fclose(inp);
getchar();
return (0);
}
/*myoutput
1
2
3
4
5
6
7
8
9
The mean is 0.45
The standard deviation is 1.34.
*/
Consider adopting the habit of using / instead of \\ for Windows path delimiters in C: "C:/Program Files/Miracle C/temperature.txt" instead of ""C:\\Program Files\\Miracle C\\temperature.txt". It always works, is easier to read, and is less error prone.