LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-22-2004, 01:21 AM   #1
ej25
Member
 
Registered: Nov 2004
Posts: 39

Rep: Reputation: 15
what's wrong


what's wrong with this code:
c=4;
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);

}


the wrong output looks like this :
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
 
Old 11-22-2004, 01:57 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
Umm...it's not in code tags?

You never actually declare any of the variables you're trying to use?

What output were you actually expecting?
 
Old 11-22-2004, 02:10 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
what's wrong with this code:
It's not indented ...
 
Old 11-22-2004, 05:04 AM   #4
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
This is part of my code, I declared every thing.

the output should look similar to this:

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
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 2
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 3
a:1
a:4
a:9
a:19
a:8
a:10
a:10
a:9
a:15
a:10
95
9.500000


but with different number in each section

this code only execute this part after the correct execution of section 1

printf("%d\n",Sum1);
aver1 = Sum1 / (float) ii;
printf("%f\n",aver1);
 
Old 11-22-2004, 05:47 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Can you tell what is your code supposed to do (not to output) ?
 
Old 11-22-2004, 11:54 AM   #6
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
my code read from a file which has four column and 10 rows then it print the sum and average of each column
 
Old 11-22-2004, 12:20 PM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Okay, can you post your full real code, as the first one posted has typos and won't compile.
 
Old 11-22-2004, 12:23 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
And don't forget to use code tags!
 
Old 11-23-2004, 04:09 AM   #9
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
this the 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;
int random[c][10];
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;


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");}

  
  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);

}
close(fd);   
        return (0);

         
    }
 
Old 11-23-2004, 08:26 AM   #10
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
This is definitively wrong:
Code:
int random[c][10];
 
Old 11-23-2004, 11:37 AM   #11
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
whay???????
 
Old 11-23-2004, 11:45 AM   #12
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You haven't initialized c to anything. You can't tell the array to be of size c when c is some uninitialized, random value.
 
Old 11-23-2004, 12:05 PM   #13
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
if I initialized c to 0 it give me "Segmentation fault"
and if I initialized to any number the same problem still
 
Old 11-23-2004, 12:10 PM   #14
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're going to need to do this with memory allocation to do it right. Otherwise you're going to need to put an upper limit on how many sections and how many quizzes they can enter.
 
Old 11-24-2004, 01:20 AM   #15
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
I didn't get it !!!!!!!!!
how can I allocate memory if I don't know the value of c
when I declared an array I'm reserving a memory location, isn't it?
can you give me an example?
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
what' s wrong phoenix_fei Programming 4 01-18-2005 10:56 AM
my time is wrong and calender is also wrong Paxmaster Linux - General 6 12-16-2004 12:46 AM
What's wrong? whaase Linux - Software 7 10-20-2003 12:39 AM
What am I doing wrong? funkenbooty Linux - Newbie 14 07-26-2003 02:16 AM
What did I do wrong? yakko Linux - Newbie 7 05-02-2002 07:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration