Thread: Series of C problems

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by OctavianC
    I found the solution for the Geometrical Mean, but it took me 3 hours. All I was doing wrong was not including math.h (since I didn't know that I should do that).
    This is the kind of problem that you can ask about here, i.e., post your code and the resulting error messages from your compiler. Or, if your program compiles but has a bug, post your code, input, expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by OctavianC View Post
    Code:
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        float a, b, GM;
        printf("Insert the two numbers for the geometrical mean\n");
        scanf("%f", &a);
        scanf("%f", &b);
        GM = sqrt(a*b);
        printf(" The geometrical mean of %f and %f is %f\n", a, b, GM);
    }
    Looks good at least for a beginner.

  3. #18
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Quote Originally Posted by Aslaville View Post
    Looks good at least for a beginner.
    Thank you. Told you I'm a fast learner .

  4. #19
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    This is the kind of problem that you can ask about here, i.e., post your code and the resulting error messages from your compiler. Or, if your program compiles but has a bug, post your code, input, expected output and actual output.
    Ok, thank you, I will be constantly posting here in the following week.

  5. #20
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Quote Originally Posted by Aslaville View Post
    With this problems the only thing I could tell you to do is (Friday is a week off )

    Well, I am hoping you left out the details since you were too lazy to type
    Actually, that's exactly how she sent us the e-mail . I just copied and pasted it.

  6. #21
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    *Note: When my teacher says "vector" she actually means "Array".

  7. #22
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Good morning!

    I have this code for union of two arrays:

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
        int A[10], B[10], C[10], i, j, k = 0, n, m, flag = 0;
        printf("How many elements does array A have?\n");
        scanf("%d", &n);
        printf("Please enter the elements of array A\n");
        for (i = 0; i<n; i++)
        {
            scanf("%d", &A[i]);
        }
        printf("How many elements does array B have?\n");
        scanf("%d", &m);
        printf("Please enter the elements of array B\n");
        for (j = 0; j<m; j++)
        {
            scanf("%d", &B[j]);
        }
        for (i = 0; i<n; i++)
        {
            C[k] = A[i];
            k++;
        }
        for (i = 0; i<n; i++)
        {
            flag = 0;
            for (j = 0; j<m; j++)
            {
                if (B[i] == C[j])
                {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0)
            {
                C[k] = B[i];
                k++;
            }
        }
        printf("\n The union of A and B is:\n");
        for (i = 0; i<k; i++)
        {
            printf("%d", C[i]);
        }
        getch();
    }
    The problem is as follows: The program runs, but when I give for example 2 elements in A: 1 and 2, and 3 elements in B: 2, 3 and 4 it returns the union as 123, when it should be 1234. Where did I go wrong? Also, how exactly does "flag" work?

  8. #23
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    I modified it a bit and it's working with 4 elements but when i give A: 1 2 3 and B: 1 2 3 4 5 it shows up as Union: 1 2 3 4

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    What is your current code?

    By the way, in the future it would be best to start a new thread for a new topic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Jan 2015
    Posts
    33
    Quote Originally Posted by laserlight View Post
    What is your current code?

    By the way, in the future it would be best to start a new thread for a new topic.
    I found a solution, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What is this series?
    By shansajid in forum C Programming
    Replies: 4
    Last Post: 01-15-2013, 05:26 AM
  2. Help me with this series
    By NoUse in forum C Programming
    Replies: 6
    Last Post: 01-22-2009, 11:57 PM

Tags for this Thread