# totally lost.

• 02-03-2011
ain
totally lost.
hi there.so my assignment is to write a program that asks the user to supply 3 integers k,m,n with k being greater than 1.and the program should compute all integers between m and n that are divisible by k.

so this is the coding:

int main()
{

int m, k, n, x ;

scanf("%d %d %d",&m,&n,&k);

if (k >1)
{
if(m<n)
{
int sum = 0;

for(x = m; x <= n; x++)
{
sum += x;

}

sum /k ;
printf("The result is %d\n",sum/k);
}

else
{
int sum = 0;

for(x = n; x <= m; x++)
{
sum += x;

}

sum /k ;
printf("The result is %d\n",sum/k);
}
}
else
printf("Invalid number\n\n");

system("PAUSE");

return 0;
}

so i actually write the right coding but the next question is the major problem for me..

the question is to upgrade above program to have an option to read the 3 integers from the INPUT DATA FILE.and then after doing the similar computation with the above, write the results in the output data file.the data file is given but i dont know if it is necessary for me to include it in here.

honestly, i dont understand anything about the 2 question so i have google about this, try copy others work and change some part to fix to my question but it doesnt work.so i really need your help to at least tell me what to do first because im sick of reading books that too 'high-level english' for me and im not a native speaker.

• 02-03-2011
esbo
OK I think I understand you.

You just have to read the data from a file instead of from the keyboard.
Code:

```FILE *input_file input_file=fopen("INPUT DATA FILE", "r"); fscanf(input_file,"%d %d %d",&m,&n,&k); //read values  trom  file fclose(input_file);```
then for output something like this.

Code:

```outputfile=fopen("results.txt","w"); fprintf(outputgile, "The result is %d\n",sum/k); flose(outputfile);```
• 02-03-2011
anduril462

Quote:

Originally Posted by ain
Code:

`          sum /k  ;`

This statement doesn't have any effect because you never store the result anywhere. You can eliminate this from the two spots in your program.

Quote:

so i actually write the right coding but the next question is the major problem for me..
Unfortunately, you don't have the right code, at least not according to the requirements. You are supposed to compute integers between n and m that are divisible by k. That is, integers for which x/k has no remainder, or x%k is zero (% is the modulus or remainder operator). Instead, you compute the average of all integers from m to n inclusive. But there's even a problem with that code, since you just do sum/k which is integer division, and gives incorrect results.

Quote:

the question is to upgrade above program to have an option to read the 3 integers from the INPUT DATA FILE.and then after doing the similar computation with the above, write the results in the output data file.the data file is given but i dont know if it is necessary for me to include it in here.

honestly, i dont understand anything about the 2 question so i have google about this, try copy others work and change some part to fix to my question but it doesnt work.so i really need your help to at least tell me what to do first because im sick of reading books that too 'high-level english' for me and im not a native speaker.
So there are equivalents to scanf and printf that work with files in general, and not just the standard input and output "files" (keyboard and monitor/terminal). You should look at fopen, fscanf, fprintf and fclose.

Quote:

Actually, your english is pretty darn good. I had no trouble following what you said.
• 02-03-2011
ain
Quote:

Originally Posted by esbo
OK I think I understand you.

You just have to read the data from a file instead of from the keyboard.
Code:

```FILE *input_file input_file=fopen("INPUT DATA FILE", "r"); fscanf(input_file,"%d %d %d",&m,&n,&k); //read values  trom  file fclose(input_file);```
then for output something like this.

Code:

```outputfile=fopen("results.txt","w"); fprintf(outputgile, "The result is %d\n",sum/k); flose(outputfile);```

so do i need to make a new function to include all this code? and {fopen("INPUT DATA FILE", "r")}what is the meaning of INPUT DATA FILE?is it where should i put my file's name?for example if my file named data_2 so i just have to replace it?

thank you.i think i understand now the general idea.but still..
• 02-03-2011
CommonTater
When in doubt look up the function in your docs... or search for it with google... It's a good habit to get into because there's no way you're going to remember even 1/10th of the stuff in the C libraries...
• 02-03-2011
ain
Quote:

Originally Posted by anduril462

This statement doesn't have any effect because you never store the result anywhere. You can eliminate this from the two spots in your program.

Unfortunately, you don't have the right code, at least not according to the requirements. You are supposed to compute integers between n and m that are divisible by k. That is, integers for which x/k has no remainder, or x%k is zero (% is the modulus or remainder operator). Instead, you compute the average of all integers from m to n inclusive. But there's even a problem with that code, since you just do sum/k which is integer division, and gives incorrect results.

So there are equivalents to scanf and printf that work with files in general, and not just the standard input and output "files" (keyboard and monitor/terminal). You should look at fopen, fscanf, fprintf and fclose.

Actually, your english is pretty darn good. I had no trouble following what you said.

oh thank you for telling me that.this is my 1 post actually.

so what do you mean is i should change sum/k to sum%k?i already compile the code and i dont find any incorrect result.

fopen, fscanf?we use this when we want the program to read from file right?

oh thank you!
• 02-04-2011
esbo
Quote:

Originally Posted by ain
so do i need to make a new function to include all this code? and
Code:

`{fopen("INPUT DATA FILE", "r")}`
what is the meaning of INPUT DATA FILE?is it where should i put my file's name?for example if my file named data_2 so i just have to replace it?

thank you.i think i understand now the general idea.but still..

You don't really need a new function, but you can make it a function if you want.

Personally I would not bother as i like to do the minimum work.

DATA_INPUT FILE is just a file you will have to create with the data in.
You can call it any name you choose.

"is it where should i put my file's name?for example if my file named data_2 so i just have to replace it?" YES!!
• 02-04-2011
esbo
Quote:

Originally Posted by ain
oh thank you for telling me that.this is my 1 post actually.

so what do you mean is i should change sum/k to sum%k?i already compile the code and i dont find any incorrect result.

fopen, fscanf?we use this when we want the program to read from file right?

oh thank you!

fopen, fscanf, yes it is very similar to reading from the keyboard. or writing to a file as the keyboard and monitor are treated as files anway (stdin stdout)

For example

fprintf(stdout,"Hello");

is the same as

printf("Hello").

Stdin and stdout are automatically opened by the program but you have to open any other files yourself.

Hence switch between the keyboard and screen to files is not too difficult.
• 02-04-2011
anduril462
Quote:

Originally Posted by ain
so what do you mean is i should change sum/k to sum%k?i already compile the code and i dont find any incorrect result.

Not exactly. The question I have is what exactly is this program supposed to do? In your first post, you said the program should:

Quote:

Originally Posted by ain
compute all integers between m and n that are divisible by k.

But your program doesn't do that. It does something else entirely, and yet you say it's correct. Do you mean it compiles without errors or warnings and prints out some number when it's done? Or do you have test data with correct answers so that you can compare your output to the expected output?

Here is an example of what I would expect, according to the requirements in your first post:
Input: k = 3, m = 7, n = 32
Output: 9 12 15 18 21 24 27 30

Notice that all the output numbers are between m and n, and are divisible by k.
• 02-04-2011
ain
Quote:

Originally Posted by anduril462
Not exactly. The question I have is what exactly is this program supposed to do? In your first post, you said the program should:

But your program doesn't do that. It does something else entirely, and yet you say it's correct. Do you mean it compiles without errors or warnings and prints out some number when it's done? Or do you have test data with correct answers so that you can compare your output to the expected output?

Here is an example of what I would expect, according to the requirements in your first post:
Input: k = 3, m = 7, n = 32
Output: 9 12 15 18 21 24 27 30

Notice that all the output numbers are between m and n, and are divisible by k.

oh YES!i do it wrong.i think i dont really understand the question. i really need to stay awake all night as i have to submit this tomorrow. thank you so much for telling me my mistake. i have to start it all over again eventhough i know im not going to finish it.
• 02-04-2011
ain
Quote:

Originally Posted by ain
oh thank you for telling me that.this is my 1 post actually.

so what do you mean is i should change sum/k to sum%k?i already compile the code and i dont find any incorrect result.

fopen, fscanf?we use this when we want the program to read from file right?

oh thank you!

thank.sorry but i dont understand this line "FILE *input_file"..this is a pointer right?input_file is what?is it my file's name?
• 02-04-2011
anduril462
No, input_file is the name of the variable. It is of type pointer to FILE. When you call fopen and it succeeds, it allocates memory for a FILE object and returns you a pointer to that object. You assign that to your file handle, like so:
Code:

```FILE *input_file; input_file = fopen("the_name_of_my_input_file", "r");  // the "r" is for reading if (input_file == NULL) {     // we have an error, print a message and exit } fscanf(input_file, ...); fclose(input_file);```
• 02-06-2011
ain
Code:

```         #include<stdio.h> int main() {     int m, n, k, sum=0;         FILE *inp;         inp = fopen ("data_1.txt", "r");         printf("data_1");       fscanf(inp,"%d %d %d",&m,&n,&k); //read values  from  file         while(m<=n)         {                 if (m%k==0)                         sum=sum+m;                 else                         sum=sum;                 m++;         }                         printf("sum=%d\n", sum);         fclose(inp);         system("PAUSE");     return 0; }```
is it something like this?it has no problem during compile but it cannot execute..