Thread: totally lost.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    7

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

    printf("Please enter 3 value: \n");
    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.

    yes, my eng is bad.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    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);
    Last edited by esbo; 02-03-2011 at 03:31 PM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please use code tags in the future, so your code is readable.

    Quote Originally Posted by ain View Post
    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.

    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.

    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.

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

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    7

    Smile

    Quote Originally Posted by esbo View Post
    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..

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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...

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    Please use code tags in the future, so your code is readable.


    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!

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ain View Post
    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!!

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by ain View Post
    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.

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by ain View Post
    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 View Post
    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.

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by anduril462 View Post
    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.

  11. #11
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    Quote Originally Posted by ain View Post
    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?

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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);

  13. #13
    Registered User
    Join Date
    Feb 2011
    Posts
    7
    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..
    Last edited by ain; 02-06-2011 at 05:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Concepts - not totally lost
    By Elysia in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2010, 04:13 PM
  2. HELP!! NEw to Arrays/Functions.. LOST
    By felixgun in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2006, 01:20 PM
  3. I lost my laptop, DVD and money
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-01-2004, 07:13 PM
  4. Totally lost beginner
    By burrissa in forum C Programming
    Replies: 7
    Last Post: 03-29-2004, 08:00 PM
  5. i'm totally lost
    By jlmac2001 in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2003, 11:06 PM

Tags for this Thread