Here is the question:
Write a C program that will be able to ceive different types of numbers and write them in appropriate files. We shall use three files file_even which is a word document, file_odd which is a text document and file_prime which is an excel file.
Using the integers from 1 to 500,
• Write all even files with their halves in file_even. Every even number should be on its own line and a number is separated from its half with a tab.
• Write all odd numbers with their squares in file_odd. Every odd number should be on its own line and it is separated from its square with a tab
• Write all prime numbers and their thirds (rounded to 2 decimall places) in file_prime. Every prime number should be on its own line and a prime number and its third should be in separate cells (equivalent of tabs)
and here is my code:
Expand|Select|Wrap|Line Numbers
- #include<stdlib.h>
- #include<stdio.h>
- /* A doc file file_even*/
- FILE *file_even;
- FILE *file_odd;
- FILE *file_prime;
- /* declaring the arrays to store the values to be written to files */
- int i;
- int odd_num[];
- int even_num[];
- int prime_num[];
- float even_halves[];
- int odd_squares[];
- float prime_thirds[];
- /* declaring the functions to be used */
- float half(int i);
- int square(int i);
- float third(int i);
- int main(void){
- /* working with the files */
- /* odd numbers */
- int odd;
- for(odd=1;odd<=500;odd++){
- if(odd%2==1){odd_num[i]=odd;i++;}
- /*even numbers */
- int even;
- for(even=1;even<=500;even++){
- if(even%2==1){even_num[i]=even;i++;}
- /*prime numbers */
- int j,i=1,c=0;
- while(i<=500){
- for(j=1;j<=500;j++){
- if(j%i==0){c++;}
- if(c==2){prime_num[a]=j;a++;}
- }
- }
- for(i=0;i<500;i++){
- even_halves[i]=half(even_num[i]);
- odd_squares[i]=square(odd_num[i]);
- prime_thirds[i]=third(prime_num[i]);
- }
- /* storing the even numbers in a file */
- file_even=fopen("file_even.doc","a");
- for(i=0;i<500;i++){
- fprintf(file_even,"%d\t%f\n",even_num[i],even_halves);
- }
- fclose(file_even);
- /* storing the odd numbers in a file */
- file_even=fopen("file_odd.txt","a");
- for(i=0;i<500;i++){
- fprintf(file_odd,"%d\t%f\n",odd_num[i],odd_squares[i]);
- }
- fclose(file_odd);
- /* storing the prime numbers */
- file_even=fopen("file_prime.xls","a");
- for(i=0;i<500;i++){
- fprintf(file_prime,"%d\t%f\n",prime_num[i],prime_thirds[i]);
- }
- fclose(file_prime);
- }
- float half(int i){
- return (i/2.0);
- }
- int square(int i){
- return (i*i);
- }
- float third(int i){
- return (i/3.0);
- }
Any one wanna tell me where the problem might be ? because it is only faced with that one error