Hello new to to the forum and also to programmig.

This is a discussion on Hello new to to the forum and also to programmig. within the C Programming forums, part of the General Programming Boards category; Hello my name is Kepal, this is my first post and I am taking my first programming class which is ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Hello new to to the forum and also to programmig.

    Hello my name is Kepal, this is my first post and I am taking my first programming class which is Intro to C. Long story short I am having trouble with loops and how to find the factors of a certain number that is imputed with a certain amount of test cases that is entered first. This is what I have, any help to the algorithms or functions to factorize a number for each test case?

    Code:
    #include <stdio.h>
    
    
    
    
    int main() {
            int T, N, a, b, curCase;
            scanf( "%d", &T);
            curCase = T >=1;
    
    
            scanf("%d", &N);
            for(curCase = 1, curCase <=1, curCase++);
                printf("Case %d: %d\n", curCase ,N);
                printf("(%d,%d\n)" , a, b);
    
    
    
    
                    printf("\n")
    
    
    
    
    
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    please any help is appreciated i am new and finding this very difficult

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,919
    One thing I suggest is that you start using braces with your control statements. Without the braces only one line following the statement will be part of the statement. Also putting a semicolon after your for statement signifies that there are no statements following that are part of that statement.

    Code:
            for(curCase = 1, curCase <=1, curCase++); // This semicolon is probably a mistake.
                printf("Case %d: %d\n", curCase ,N);  // Without the semicolon only this line will be executed within the loop.
                printf("(%d,%d\n)" , a, b);
    Jim

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Also it helps to turn on higher warnings (diagnostics) in your compiler. For example, I notice you are printing a and b, but you never have assigned those a value. Maybe you are forgetting a step in your logic?

    Finally when solving a problem it helps to say an example given input and what the expected output should be. For example, if your program is supposed to multiply A and B, then an example input is 4, 3 for which we know the answer. If you don't get the ouptut of 12, then you made a mistake in the program. If you do get 12, on the other hand, then it means your solution *might* be correct but not necessarily.

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thank you so like this?

    Code:
    scanf("%d", &N);
            for(curCase = 1; curCase <=1 ; curCase++)
            {
                printf("Case %d: %d\n", curCase ,N);
                printf("(%d,%d\n)" , a, b);
    
    
                printf("\n");
            }
    How would I make it so in the second to last printf statement will actully print out a and b as factors of N? do i have to use another loop statement for N?

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Where are you assigning the values for a and b? If you want a and b to be factors of N, then you need to choose a and b such that a * b == N.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Quote Originally Posted by c99tutorial View Post
    Also it helps to turn on higher warnings (diagnostics) in your compiler. For example, I notice you are printing a and b, but you never have assigned those a value. Maybe you are forgetting a step in your logic?

    Finally when solving a problem it helps to say an example given input and what the expected output should be. For example, if your program is supposed to multiply A and B, then an example input is 4, 3 for which we know the answer. If you don't get the output of 12, then you made a mistake in the program. If you do get 12, on the other hand, then it means your solution *might* be correct but not necessarily.
    well the program is not supposed to ask for an input, just begins with an input of T = the number of cases, then the next line will be an integer N = the number that will be factorized. the next like is supposed to be printed as Case : N then the next line will be (a,b) which will be the factors of the number entered.

    hope that makes sense. I am kinda in the middle of no where with this and really appreciate all the input thank you

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    1,220
    Quote Originally Posted by spraypaul View Post
    well the program is not supposed to ask for an input, just begins with an input of T = the number of cases, then the next line will be an integer N = the number that will be factorized.
    In that case, N is your input (The number that will be factorized). Whether this is read from the user or not, it doesn't matter. For example, you should be able to think of an example for N and you should be able to say what you expect the result to be, given that N. Then, your job is simply to make the program give you the correct result when it has that N.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Quote Originally Posted by c99tutorial View Post
    In that case, N is your input (The number that will be factorized). Whether this is read from the user or not, it doesn't matter. For example, you should be able to think of an example for N and you should be able to say what you expect the result to be, given that N. Then, your job is simply to make the program give you the correct result when it has that N.
    Thats the confusion that I am having. I'm not sure how to enter the equation to find the factors of n and where they should go. and also say i enter 4 for the case number how do i make sure that the it will print out the number of cases that are specified when you enter T.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    I think your biggest problem is that you can't clearly describe what your program should do. At least I don't understand what you are supposed to do. So please explain in simple English, what your program should do.

    For example suppose T = 2 and N = 4. What is the output you expect and why?

    It will probably also help if you post the exact assignment text.

    Bye, Andreas

  11. #11
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Quote Originally Posted by AndiPersti View Post
    I think your biggest problem is that you can't clearly describe what your program should do. At least I don't understand what you are supposed to do. So please explain in simple English, what your program should do.

    For example suppose T = 2 and N = 4. What is the output you expect and why?

    It will probably also help if you post the exact assignment text.

    Bye, Andreas
    write a program that takes in a series of integers, N, and prints out all pairs of numbers (a,b), such that a times b equals N, such that a is less than or equal to b, and a is not equal to 1. Furthermore, you will print out a special message if the number is a prime.

    Input spec: begin with a line single integer, T (1 < T < 10). On each of the following T lines, there will be a single integer, N (2 < N < 10,000,000).

    Output specs:For each test case, your program should first print a case header. This will begin with

    Case X: N

    Where X is the case number (from 1 to T), and N is the number which will be factorized. After this line, print a single line for each pair (a,b) such that a is less than or equal to b, a is not equal to 1, and a times b equals N. These lines should have the following format

    (a,b)

    These lines should be printed in increasing order of a. Be careful to print each pair exactly once.

    Code:
    #include <stdio.h>
    
    
    
    
    int main() {
    
    
            int T, N, a, b, curCase;
            curCase = T >=1;
            N = 2 <= N <= 10000000;
            T = 1 <= T <= 10;
            
    
    
            scanf("%d", &T);
    
    
            for(curCase = 1; curCase <= T ; curCase++)
            {
    
    
                scanf("%d", &N);
               printf("Case %d: %d\n", curCase ,N);
                printf("(%d,%d\n)" , a, b);
    
    
    
    
            printf("\n");
    
    
            printf("\n");
    
    
            }
    Thats what i have so far. I dont know what formula i need to find the factor of the number, or where it should go.

    thank you, kepal

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Help!

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by spraypaul View Post
    Input spec: begin with a line single integer, T (1 < T < 10). On each of the following T lines, there will be a single integer, N (2 < N < 10,000,000).
    Are you supposed to read the data from a file?
    If the input comes from the keyboard, you need an array to store the numbers (have you learned about arrays yet?).

    Or do you mix input and output, like:
    Code:
    3                 // number of test cases
    450               // first number
    Case 1: 450
    (pair1)
    (pair2)
    ...
    2343              // next number
    Case 2: 2343
    (pair1)
    (pair2)
    ....
    IMHO the description of the assignment is rather ambigous.

    Quote Originally Posted by spraypaul View Post
    Thats what i have so far. I dont know what formula i need to find the factor of the number, or where it should go.
    How would you find the factors by hand?

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    No, We haven't done arrays and you are supposed to enter a number of cases first
    Code:
    
    
    
    int main() {
    
    
            int T, N, a, b, curCase;
            curCase = T >=1;
            N = 2 <= N <= 10000000;
            T = 1 <= T <= 10; 
    
    
    
            scanf("%d", &T); // this is scaning for num of test cases
    
    
            for(curCase = 1; curCase <= T ; curCase++)
            {
    
    
                scanf("%d", &N); // this is supposed to be the first integer tested, but the number of integers is going to depend on how many test cases there are..
               printf("Case %d: %d\n", curCase ,N);
                printf("(%d,%d\n)" , a, b);
    
    
    
    
            printf("\n");
    
    
            printf("\n");
    so if you type in 4 for the number of test cases it is going to ask for 4 diff numbers like 9,10,11,12.. then the out put is supposed to be the factors of that number like

    Case 1: 9
    (3,3)

    Case 2 : 10
    (2,5)

    Case 3: 11
    This number is prime!

    Case 4: 12
    (2,6)
    (3,4)

    also i know i need a if statement for the prime number but i am also unsure where that goes. Sorry if this is really confusing like I said this is my first programming class so really in the blue right now.

    thanks
    kepal
    Last edited by spraypaul; 02-10-2013 at 02:51 PM.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,778
    Write code to find out if a number is prime.
    Until you can do that you do not have a chance at doing this assignment.
    After writing a function to determine if a number is prime; than take this code and create a new function (possibly called factor) to do this assignment.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General forum question - if in wrong forum...
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 06:00 AM
  2. Having a second forum pop-up
    By 7smurfs in forum C# Programming
    Replies: 0
    Last Post: 03-11-2005, 08:10 PM
  3. programmig help!
    By dean in forum C++ Programming
    Replies: 7
    Last Post: 06-03-2004, 03:25 PM
  4. Replies: 7
    Last Post: 09-08-2002, 03:20 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21