# Thread: Need help in C

1. ## 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.

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

3. 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.

4. 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.

5. im having trouble understanding what you mean by
"Max sublist is {1,2,3} "

6. 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```

7. 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?

8. It wouldn't matter after that point. As long as the MaxSum is the same.

9. 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. 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.

#### Posting Permissions

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