Segment woes

This is a discussion on Segment woes within the C Programming forums, part of the General Programming Boards category; Code: //TJ Wright 11 3 #include <stdio.h> int main () { int m, count=0,n[30]; printf("Enter the numbers, one per line"); ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Segment woes

    Code:
    //TJ Wright 11 3
    #include <stdio.h>
    int main ()
    {
    int m, count=0,n[30];
    
    printf("Enter the numbers, one per line");
    
    for(m=1;m<=20;m++)
    {
    scanf("%d",&n[m]);/* input up to 20 integers, one per line, the sentinel is 0
    		  each of the numbers will be less than 2^30.*/
    count++;
    if(n[m]==0)break;
    }
    
    for (m=1;m<=count;m++)
    {
    /*
    
    if statements asking if there is a number n such that all the integers
    are in the segment [n, n+32767]. If there is such a number, print the largest
    such n. If there is not, determine if there is a number n such that
    all but  1 of the integers is in the segment [n,n+32767]. If there is such
    a number, print the largest such. If there is no such, determine if there is
    such an n so that all but two of the intervals are in the interval and print
    the largest such. Continue this way until n is found.*/
    }
    
    return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    arrays are zero based (indexes start at 0 not 1)
    for 20 numbers you need array of 20 elements, not 30

    What is your problem here?

    You can start for example (if you don't know how to solve the problem) with determining the min and max of all numbers... Then check the differense. If differense is less than 32767 - min number is your n

    (You may want to sort array - so the min and max will be first and last element)

    You may want to use long instead of int - to make your program portable...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. execute a code in data segment...
    By ravindragjoshi in forum Windows Programming
    Replies: 1
    Last Post: 06-12-2007, 11:43 PM
  3. Segment woes
    By astrodude15 in forum C Programming
    Replies: 3
    Last Post: 06-01-2007, 10:10 AM
  4. Declare an array in the BSS segment
    By ^xor in forum C Programming
    Replies: 1
    Last Post: 05-27-2005, 05:12 PM
  5. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM

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