473,396 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Could somebody help me find the cause of a Segmentation Fault error in my program?

The program compiles and works all the way until the multiplecheck function, any help would be great. The program is not finished yet and I am new to C programming so I know the program may not be the most efficient.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<math.h>
  3. #define ARRAYSIZE 30
  4.  
  5. int getData(int[]);
  6. void printData(int[],int);
  7. int multipleCheck(int[],int);
  8. int multipleCount(int[],int,int,int);
  9.  
  10. int main()
  11. {
  12.   int data[ARRAYSIZE];
  13.   int num_data;
  14.   int multi_ct;
  15.  
  16.   num_data = getData(data);
  17.   printData(data, num_data);
  18.   multi_ct = multipleCheck(data,num_data);
  19.   return(0);
  20. }
  21.  
  22. int getData(int array[])
  23. {
  24.   int lcv; //local counting variable
  25.   int ct; //the number of times that the user enters a number
  26.  
  27.   lcv = 0;
  28.   ct = 1;
  29.   while (lcv < ARRAYSIZE)
  30.   {
  31.     printf("Enter the data #%d or -1: ", lcv + 1);
  32.     scanf("%d", &array[lcv]);
  33.     ct++;
  34.     if (array[lcv] < 0)
  35.     {
  36.       array[lcv] = 0;
  37.       lcv = 49;
  38.       ct--;
  39.     }
  40.     lcv++;
  41.   }
  42.   printf("\n");
  43.   return(ct);
  44. }
  45.  
  46.  
  47. void printData(int array[],int num_d)
  48. {
  49.   int lcv;
  50.  
  51.   printf("Data entered: ");
  52.   for (lcv = 0; lcv < (num_d - 1); lcv++)
  53.   {
  54.    printf("%d ", array[lcv]);
  55.  
  56.   }
  57.   printf("\n");
  58. }
  59.  
  60.  
  61. int multipleCheck(int array[], int num_d)
  62. {
  63.   int lcv;
  64.   int lcv2;
  65.   int ct;
  66.   int ctmax;
  67.   int hold;
  68.  
  69.   ctmax = 0;
  70.  
  71.   for (lcv = 0; lcv < (num_d - 2); lcv++)
  72.   {
  73.     for (lcv2 = lcv + 1; lcv2 < (num_d - 1); lcv2++)
  74.     {
  75.       hold = array[lcv] * array[lcv2];
  76.       ct = multipleCount(array,lcv,hold,num_d);
  77.       if (ct > ctmax)
  78.       {
  79.         ctmax = ct;
  80.       }
  81.     }
  82.   }
  83.   printf("\n%d\n", ctmax);
  84.   return (ctmax);
  85. }
  86.  
  87.  
  88. int multipleCount(int array[], int start, int num_p,int num_d)
  89. {
  90.   int lcv;
  91.   int lcv2;
  92.   int ct;
  93.  
  94.   ct = -1;
  95.  
  96.   for (lcv = 0; lcv <= (num_d - 1); lcv++)
  97.   {
  98.     for (lcv2 = lcv + 1; lcv <= num_d; lcv2++)
  99.     {
  100.       if ((array[lcv] * array [lcv2]) == num_p)
  101.       {
  102.         ct++;
  103.       }
  104.     }
  105.   }
  106.   return(ct);
  107. }
  108.  
Apr 18 '10 #1
1 1596
newb16
687 512MB
line 98: condition for loop is wrong: for (lcv2 = lcv + 1; lcv <= num_d; lcv2++)
Apr 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: diyanat | last post by:
i am writing a cgi script in C using the CGIC library, the script fails to run, i am using apache on linux error report from apache : internal server error Premature end of script headers:...
9
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
21
by: user | last post by:
I just finish writing LAB2 with no errors when compiling, but once i run it, i get "segmentation fault".. i don't know what is wrong, can anyoen tell me what it means and how i can fix it? thx!
3
by: I_have_nothing | last post by:
Hi! I am new in C. I got a lots of "Segmentation Fault"s in my code. I guess One possibility is: if " int array_i; " is declard and the code trys to access "array_i", a Segmentation Fault will...
3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
0
by: justarrived | last post by:
Hi, I am working on a Pro-c program on Unix. It compiles/builds properly but while trying to run it I am receiving segmentation violation at- sqlcxt((void **)0, &sqlctx, &sqlstm, &sqlfpn); The...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.