Results 1 to 10 of 10

Thread: Need help in C

  1. #1
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412

    Need help in C

    I'm writing a program for school that takes in an even number and returns two prime numbers that sum to equal that number. Here is what I have so far:

    #include<stdio.h>
    int p1;
    int p2;
    main() {
    printVerification(4,100,0);
    }
    //Checks to see if a number "n" is prime.
    int checkPrimer(int n){
    if((n<2) || ((n>2) && ((n%2) == 0)))
    return 0;
    else{
    int index=2;
    while(index<n){
    if((n%index)==0)
    return 0;
    else
    index++;
    }
    return 1;
    }
    }
    //finds the next primer > n.
    int findNextPrimer(int n){
    int n1 = n + 1;
    while(checkPrimer(n1) == 0)
    n++;
    return n1;
    }

    //Given an enen number e, find two primers p1 and p2
    //such that e=p1+p2. For examples, 4=2+2, 10=3+7, and
    //12=5+7.
    void onePlusOne(int e) {


    }
    //It prints out all expressions
    //e=p1+p2 for all even numbers e between
    //e1 and e2. The number width is used to
    //control the number of expressions at each row
    int printVerification(int e1,int e2,int width){
    int index = e1;
    while((index>=e1)&&(index<=e2)) {
    onePlusOne(index);
    printf("%d plus ",p1);
    printf("%d equals ",p2);
    printf("%d \n",index);
    index++;
    }




    Can someone help me out with this onePlusOne method? I can't figure out how to implement this. I have until monday to finish this and I'm stuck on this method.
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

  2. #2
    Constant Bitrate OdysseyPC's Avatar
    Join Date
    Oct 2003
    Location
    ACT, Australia
    Posts
    114
    I admire your resourcefulness, so much so here's an answer.
    I changed the void return to an int return giving a 0 for success and a 1 for otherwise.
    Code:
    int onePlusOne(int e)
    {
      if ((e%2) != 0)
        return 1; // Not an even number 
    
      p1 = findNextPrimer(1);
      p2 = findNextPrimer(1);
    
      while ((p1+p2) != e)
      {
        if ((p1>=e) && (p2>=e))
          return 1;  // Fail
        elseif (p1>=e)
        {
          p1 = findNextPrimer(1);
          p2 = findNextPrimer(p2);
        }
        else
          p1 = findNextPrimer(p1);
      }
     
      return 0; // Success 
    }
    It's not clean by any means but either is your code and it works
    Caputer Mk. II
    '02 VX Holden Commodore Series II Executive
    MII12000, 512MB RAM, 60GB HDD (5400rpm), 16X DVD, TS200V
    Morex 60W DC-DC, Custom S/SDC
    OS/Software: Developing...

  3. #3
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412
    Thanks, I actually wasn't looking for an answer just for guidance on how to figure out the algorithm. I actually figured out the method on my own and its pretty similar to your approach. Again, thanks for the help. Now I have to clean this thing up, document it, and write a gui for it to email to my professor before thursday.
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

  4. #4
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412
    I need help again:

    I'm writing another program that takes a list of integers as input and computes a the maximum sum of a sublist of these integers.

    It also needs to compute the start index and end index of this sublist.

    ex:

    List {1,2,3,-5,4}

    Max sublist is {1,2,3}
    MaxSum = 6
    start index = 0. end index =3.

    Here is what I have so far.

    Code:
    public void computeMaxSum(DefaultList<Integer> list) {
        	List = list;
            int n = List.size();
            int i;       // index for the list
            maxSum = 0;
            int maxTail = 0;
            for (i=0 ; i < n; i = i+1) {
                if (maxTail > 0)
                    maxTail=maxTail + List.get(i); 
                    
                else{
                    maxTail = List.get(i);
                    
                    
                }
                if (maxTail > maxSum) {
                    maxSum = maxTail;
                    
                    
                }
                   
            }
                setChanged();
                notifyObservers();
        }
    It computes the maxSum ok but I can't figure out how to get the startIndex and endIndex.
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

  5. #5
    My man uses Levitra. Peoples's Avatar
    Join Date
    Dec 2004
    Location
    Maryland
    Posts
    2,024
    im having trouble understanding what you mean by
    "Max sublist is {1,2,3} "
    PC Components:
    Lilliput; XPC/FLEX mobo; 1.7 ghz P4 Mobile;512 DDR; 160 gb HDD; opus 150; slot usb dvd-rw
    My work log

  6. #6
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412
    Here is a better explanation:

    Define a sublist of a list l to be a list containing 0 or more contiguous elements
    of l. For instance, the list (1, 2, 3) has seven sublists: the empty sublist; three sublists of length 1: (1), (2), and (3);
    two sublists of length 2: (1, 2) and (2, 3);
    one sublist of length 3: (1, 2, 3).

    The problem we want to solve is this: given a list of integers, find the maximum
    sum of the elements of a sublist. For example, the following lists have sublists
    with maximum sum as shown.

    Code:
    list                              sublist with max sum       max sum
    (-2, 4, -3, 5, 3, -5, 1)           (4, -3, 5, 3)             9
    (2, 4, 5)                              (2, 4, 5)                 11
    (-1, -2, 3)                           (3)                         3
    (-1, -2, -3)                          ()                          0
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

  7. #7
    My man uses Levitra. Peoples's Avatar
    Join Date
    Dec 2004
    Location
    Maryland
    Posts
    2,024
    curious about the seneario as such
    (1,2,5,-6,6)
    the maxsum of 1,2,5 =8
    but the maxsum of 1,2,5,-6,6 would also be 8
    would it not matter at this point?
    PC Components:
    Lilliput; XPC/FLEX mobo; 1.7 ghz P4 Mobile;512 DDR; 160 gb HDD; opus 150; slot usb dvd-rw
    My work log

  8. #8
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412
    It wouldn't matter after that point. As long as the MaxSum is the same.
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

  9. #9
    Maximum Bitrate mushin's Avatar
    Join Date
    Aug 2004
    Location
    SE Michigan
    Posts
    750
    That's not C! You're using templates and class objects!

    Your start index is when you (re)set maxTail, ie when you set maxTail = List.get(i). Your end index is when you set maxSum = maxTail. Just store the value of i at these points, and you're golden. That and handling the null answer, which I'm sure you can do.

    edit: Oops, that'd have a bug. You want to keep a temp start index, and a seperate start index for the actual maxSum.

  10. #10
    Variable Bitrate bjm2020's Avatar
    Join Date
    May 2003
    Location
    Louisiana
    Posts
    412
    its not C, I just posted it in the same thread as my other problem. Its java. Peoples helped me figure out the solution though.
    EPIA 800, DWW-7VGA LCD w/Touchscreen, 256mb ram, dvd rom, ati radeon 7000 video card, 80 gb 3.5" hard drive.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •