Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 11-28-2004, 09:05 PM   #1 (permalink)
ej25
Registered User
 
Join Date: Nov 2004
Posts: 1
ej25 is on a distinguished road
what's wrong with this code?????

Code:
#include <stdio.h> 
#include <stdlib.h>
#include <fcntl.h>             /* defines options flags */ 
#include <sys/types.h>     /* defines types used by sys/stat.h */ 
#include <sys/stat.h>        /* defines S_IREAD & S_IWRITE  */ 

int main( )
{
  FILE *fd;
  int fd2;
  int sec,quize; 
  char buffer[1024];
  char buffer2[1024];
  int a,b,n,c=0;
  int random[100][100];

 
  int i,Sum1,Sum2,ii;
  float aver1,aver2;
  int j;
  fd = fopen("input.txt","w+");
   
  printf("Enter number of sections:");
  scanf("%d",&sec);

  printf("Enter number of quizes in each section:");
  scanf("%d",&quize);
  c= sec*quize;

  random[c][10];

  for(a=0;a<c;a++){
    for(b=0;b<10;b++)
       random[a][b]=rand()%21;
  }
 
  for(b=0;b<10;b++){
    for(a=0;a<c;a++){
      fprintf(fd,"%d",random[a][b]);
      fprintf(fd,"\t");
    }
    fprintf(fd,"\n");
  }

  for(a=0;a<c;a++){
    for(b=0;b<10;b++)
      printf("random[%d][%d]=%d\n",a,b,random[a][b]);
  }
	
  fclose (fd);  
        
  fd = fopen("input.txt","r+");
  i=1;
  ii=0; 
  Sum1=0; 
  Sum2=0;
  j=0;
  for(j=0;j!=c;j++){
    printf("section number %d\n",j);
    while(ii!=10){
      fscanf(fd,"%d",&a);
      printf("a:%d\n",a);
      Sum1=Sum1+a;
      i=1;
      while(i!=c){
         fscanf(fd,"%d",&a);
         i++;
      }
      ii++;
    }
    printf("%d\n",Sum1);
    aver1 = Sum1 / (float) ii;
    printf("%f\n",aver1);
  }
  fclose(fd);   
  return (0);
}

Last edited by redhead; 11-28-2004 at 10:45 PM. Reason: Learn to use [ code] tags...
ej25 is offline   Reply With Quote
Old 11-28-2004, 11:32 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Code:
#include <stdio.h> 
#include <stdlib.h>
#include <fcntl.h>            /* defines options flags */
#include <sys/types.h>     /* defines types used by sys/stat.h */
#include <sys/stat.h>       /* defines S_IREAD & S_IWRITE  */

int main( )
{
  FILE *fd;
  int fd2;
  int sec,quize; 
  char buffer[1024];
  char buffer2[1024];
  int a,b,n,c=0;
  int random[100][100];

 
  int i,Sum1,Sum2,ii;
  float aver1,aver2;
  int j;
  fd = fopen("input.txt","w+");
   
  printf("Enter number of sections:");
  scanf("%d",&sec);

  printf("Enter number of quizes in each section:");
  scanf("%d",&quize);
  c= sec*quize;


/* 
 * what should this do ?? 
 * you're not assigning anything to that 
 * position in the random array
 */

  random[c][10];

  for(a=0;a<c;a++){
    for(b=0;b<10;b++)
       random[a][ b]=rand()%21;
  }
 
  for(b=0;b<10;b++){
    for(a=0;a<c;a++){
      fprintf(fd,"%d",random[a][ b]);
      fprintf(fd,"\t");
    }
    fprintf(fd,"\n");
  }

  for(a=0;a<c;a++){
    for(b=0;b<10;b++)
      printf("random[%d][%d]=%d\n",a,b,random[a][ b]);
  }
	
  fclose (fd);  
        
  fd = fopen("input.txt","r+");
  i=1;
  ii=0; 
  Sum1=0; 
  Sum2=0;
  j=0;
  for(j=0;j!=c;j++){
    printf("section number %d\n",j);
    while(ii!=10){
      fscanf(fd,"%d",&a);
      printf("a:%d\n",a);
      Sum1=Sum1+a;
      i=1;
      while(i!=c){
         fscanf(fd,"%d",&a);
         i++;
      }
      ii++;
    }
    printf("%d\n",Sum1);
    aver1 = Sum1 / (float) ii;
    printf("%f\n",aver1);
  }
  fclose(fd);   
  return (0);
}
First of all, what should it do..
A small test run of your code gives:
Quote:
redhead@ask{57} ~> gcc -Wall -o te test.c
test.c: In function `main':
test.c:30: warning: statement with no effect // The random[c][10];
test.c:19: warning: unused variable `aver2'
test.c:14: warning: unused variable `n'
test.c:13: warning: unused variable `buffer2'
test.c:12: warning: unused variable `buffer'
test.c:10: warning: unused variable `fd2'
redhead@ask{58} ~> ./te
Enter number of sections:2
Enter number of quizes in each section:3
random[0][0]=1
random[0][1]=4
random[0][2]=9
random[0][3]=19
random[0][4]=8
random[0][5]=10
random[0][6]=10
random[0][7]=9
random[0][8]=15
random[0][9]=10
random[1][0]=2
random[1][1]=19
random[1][2]=20
random[1][3]=4
random[1][4]=20
random[1][5]=7
random[1][6]=3
random[1][7]=15
random[1][8]=16
random[1][9]=16
random[2][0]=17
random[2][1]=14
random[2][2]=12
random[2][3]=9
random[2][4]=2
random[2][5]=5
random[2][6]=5
random[2][7]=13
random[2][8]=1
random[2][9]=19
random[3][0]=5
random[3][1]=0
random[3][2]=3
random[3][3]=12
random[3][4]=17
random[3][5]=9
random[3][6]=1
random[3][7]=7
random[3][8]=16
random[3][9]=16
random[4][0]=15
random[4][1]=18
random[4][2]=12
random[4][3]=14
random[4][4]=20
random[4][5]=10
random[4][6]=20
random[4][7]=2
random[4][8]=2
random[4][9]=15
random[5][0]=17
random[5][1]=19
random[5][2]=7
random[5][3]=8
random[5][4]=8
random[5][5]=9
random[5][6]=11
random[5][7]=11
random[5][8]=1
random[5][9]=10
section number 0
a:1
a:4
a:9
a:19
a:8
a:10
a:10
a:9
a:15
a:10
95
9.500000
section number 1
95
9.500000
section number 2
95
9.500000
section number 3
95
9.500000
section number 4
95
9.500000
section number 5
95
9.500000
redhead@ask{60} ~> cat input.txt
1 2 17 5 15 17
4 19 14 0 18 19
9 20 12 3 12 7
19 4 9 12 14 8
8 20 2 17 20 8
10 7 5 9 10 9
10 3 5 1 20 11
9 15 13 7 2 11
15 16 1 16 2 1
10 16 19 16 15 10
redhead@ask{61} ~>
Seems to me, like it is saving all the random numbers that was calculated into the input.txt file, in your reading section you do have a small error tho, when reading from a filedescriptor you read one single line at a time, that means, when reading for section 1, you read every line in the file, leaving fd pointing at EOF for the following reads.
So in the end for the reads of 2 - 6 (in this small test case) you won't have any reads.
Either you should reconsider your readin section, where you most likely will read everything in, then sum up the sections, where number of sections can be decided by number of '\t' , and number of reads per section, which can be decided by number of '\n'.
Or you should consider the design of the write to input.txt, where you would have each line representing each sections, so an array like:
Code:
random[3][5] = {
                          {1, 2, 3, 4, 5},    
                          {10, 9, 8, 7, 6},                          
                          {1, 4, 6, 2, 8}
                        };
~> cat input.txt
1    2    3    4    5
10    9    8    7    6
1    4    6    2    8
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM
whats wrong with this code trevor PHP 7 04-01-2003 06:00 PM
ok, what is wrong with this code? trevor PHP 3 02-04-2003 10:09 PM


All times are GMT -8. The time now is 04:01 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting