C Methods yielding inconsistent results - c

Firstly I am fairly familar with Java programming and have just started to learn C programming.
Below is a program that takes inputted integers and finds the min, max and sum.
To me (at least) that code written seems logical in properly performing the needed tasks and functions.
#include <stdio.h>
#include <limits.h>
int sum(int array[], unsigned int length) {
int sumTest;
for (int i = 0; i < length; i++) {
sumTest = sumTest + array[i];
}
return sumTest;
}
int max(int array[], unsigned int length) {
int maxTest = array[0];
for (int i = 0; i < length; i++) {
if (array[i] > maxTest) {
array[i] = maxTest;
}
}
return maxTest;
}
int min(int array[], unsigned int length) {
int minTest = array[0];
for (int i = 0; i < length; i++) {
if (array[i] < minTest) {
array[i] = minTest;
}
}
return minTest;
}
int main(void) {
unsigned int length = 0;
scanf("%u", &length);
int array[length];
int temp = -1;
for (int i = 0; i < length; i++) {
scanf("%d", &temp);
array[i] = temp;
}
printf("SUM: %d\n", sum(array, length));
printf("MAX: %d\n", max(array, length));
printf("MIN: %d\n", min(array, length));
return 0;
}
However when I type in the console some entries the results vary every time.
1
10
SUM: 286271434
MAX: 10
MIN: 10
Again
1
10
SUM: -72217974
MAX: 10
MIN: 10
My compiler highlights the "<" sign in the for loops in the three methods min,max and sum telling me I am comparing unsigned and signed integers. To fix this I have tried fixing the line to look like this.
for (unsigned int i = 0; i < length; i++) {
Unfortunately I still get the same results, which logically to me makes sense as I have read that unsigned integers can hold large positive numbers. I've read a bit about C being more about memory management, is it possible that I am not considering memory as a factor. I've truth fully been looking up the syntax from handbooks and forum posts none of which don't seem to consider memory as a factor and which seems no different from java.
Can someone help me understand why this happening and what a solution is.
Thank you.

You don't ever initialize sumTest to anything before using it to keep track of the sum so the value is indeterminate. Setting it to 0 should fix the issue like so:
int sum(int array[], unsigned int length) {
int sumTest = 0;
for (int i = 0; i < length; i++) {
sumTest = sumTest + array[i];
}
return sumTest;
}

Initialize sum_test=0;.
While finding the min/max instead of saving the temp value you modify the array. This should solve it:
min_test = array[i];

As the other answers have noted, sumTest needs to be initialized to 0 before use.
You might also want to test what happens with
unsigned int length = 0;
scanf("%u", &length);
int array[length];
when you enter a very large value for length.

You have not initialized the sumTest, so it takes any garbage value. To make it more meaningful initialize it to 0
i.e. int sumTest = 0;

Related

What's causing the read access violation/overflow?

I'm trying to write a program that sorts an array of size N via a selections sort and then conducts a binary search for a random number in that array and displays the index in which that number is present. I noticed that without my binary search function I begin to get a stack overflow when N is greater than 1e5 and when I try to run the binary search I run into the error "read access violation". I would greatly appreciate any help on this especially considering my N is supposed to be 1e6.
#define N 10
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function prototypes
void selectionSort(int array[], size_t length);
void swap(int* elementPtr, int* element2Ptr);
void printPass(int array[], size_t length, unsigned int pass, size_t index);
size_t binarySearch(const int b[], int searchKey, size_t low, size_t high);
unsigned long long int counter = 0;
unsigned long long int counter2 = 0;
long long unsigned int counter3 = 0;
int main(void) {
int array[N];
srand(time(NULL));
for (size_t i = 0; i < N; i++) {
array[i] = rand() % 90 + 10; // give each element a value
}
/*
puts("Unsorted array:");
for (size_t i = 0; i < N; i++) { //print the array
printf("%d ", array[i]);
}
puts("{\n");
*/
selectionSort(array, N);
/*
puts("Sorted array:");
for (size_t i = 0; i < N; i++) { //print the array
printf("%d ", array[i]);
}
*/
printf("\nTotal amount of comparisons: %d\n", counter);
printf("Total amount of swaps: %d", counter2);
int value = rand() % N + 1;
int index = binarySearch(array, value, 0, N);
printf("\nThe amount of times the value was compared was: %d\n", counter3);
if (index != -1) {
printf("%d was first found on index %d\n", value, index);
}
else printf("%d was not found on the array\n", value);
}
void selectionSort(int array[], size_t length) {
//loop over length - 1 elements
for (size_t i = 0; i < length - 1; i++) {
size_t smallest = i; //first index of remaining array
//loop to find index of smallest element
for (size_t j = i + 1; j < length; j++) {
counter++;
if (array[j] < array[smallest]) {
smallest = j;
}
}
swap(array + i, array + smallest); //swap smallest element
//printPass(array, length, i + 1, smallest); //output pass
}
}
//function that swaps two elements in the array
void swap(int* elementPtr,int* element2Ptr)
{
counter2++;
int temp;
temp = *elementPtr;
*elementPtr = *element2Ptr;
*element2Ptr = temp;
}
//function that prints a pass of the algorithm
void printPass(int array[], size_t length, unsigned int pass, size_t index) {
printf("After pass %2d: ", pass);
//output elements till selected item
for (size_t i = 0; i < index; i++) {
printf("%d ", array[i]);
}
printf("%d ", array[index]); //indicate swap
//finish outputting array
for (size_t i = index + 1; i < length; i++) {
printf("%d ", array[i]);
}
printf("%s", "\n "); //for alignment
//indicate amount of array that is sorted
for (unsigned int i = 0; i < pass; i++) {
printf("%s", "-- ");
}
puts(""); //add newline
}
size_t binarySearch(const int b[], int searchKey, size_t low, size_t high) {
counter3++;
if (low > high) {
return -1;
}
size_t middle = (low + high) / 2;
if (searchKey == b[middle]) {
return middle;
}
else if (searchKey < b[middle]) {
return binarySearch(b, searchKey, low, middle - 1);
}
else {
return binarySearch(b, searchKey, middle + 1, high);
}
}
For N as big as 1e5 or 1e6, you can't afford allocating it on stack. The size of an int is 4 bytes and so you'll consume 4e5 bytes from stack just for your array.
You will need to dynamically allocate the array and instead of
int array[N];
you should have
int *array = malloc(sizeof(int) * N);
and after you are done with everything, don't forget to
free(array);
Now you should have enough space on stack for the recursive binary search.
UPDATE:
After I've run the code myself, indeed, the binarySearch function always yields segmentation fault. The problem is the type of the parameters, namely size_t. There are cases where high argument from the binarySearch function becomes -1. But because the size_t is an unsigned type, you have an integer underflow, thus high will become maxint. So your condition if (low > high) would never become true. You'll have to change the types of low and high to a signed integer to have the function working.
Still, I suggest going for the dynamic allocation, even though your stack might cope with that.
Even outside of the great answer that was posted, I am seeing other problems with this code. I have issues running it with N = 2, N =5, and N = 10.
I believe you have some problems with passing the variables into your binary search function. I think that you are passing incorrect values that are overflowing and causing all sorts of memory nightmares. This is causing your read access violations.
Do your small cases function appropriately? Despite the suggestions to minimize your footprint. I would double check simple cases are functioning.

How to read space-separated integers representing the array's elements and sum them up in C

How to read space-separated integers representing the array's elements and sum them up in C?
I used the below code but it reads all the elements in a new line:
#include <math.h>
#include <stdio.h>
int main() {
int i = 0, N, sum = 0, ar[i];
scanf("%d" , &N);
for (i = 0; i < N; i++) {
scanf("%d", &ar[i]);
}
for (i = 0; i < N; i++) {
sum = sum + ar[i];
}
printf("%d\n", sum);
return 0;
}
Your array ar is defined with a size of 0: the code invokes undefined behavior if the user enters a non zero number for the number of items.
Furthermore, you should check the return value of scanf(): if the user enters something not recognized as a number, your program will invoke undefined behavior instead of failing gracefully.
Here is a corrected version:
#include <stdio.h>
int main(void) {
int i, N, sum;
if (scanf("%d", &N) != 1 || N <= 0) {
fprintf(stderr, "invalid number\n");
return 1;
}
int ar[N];
for (i = 0; i < N; i++) {
if (scanf("%d", &ar[i]) != 1) {
fprintf(stderr, "invalid or missing number for entry %d\n", i);
return 1;
}
}
sum = 0;
for (i = 0; i < N; i++) {
sum += ar[i];
}
printf("%d\n", sum);
return 0;
}
Note that the program will still fail for a sufficiently large value of N as there is no standard way to check if you are allocating too much data with automatic storage. It will invoke undefined behavior (aka stack overflow).
You should allocate the array with malloc() to avoid this:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int i, N, sum;
int *ar;
if (scanf("%d", &N) != 1 || N <= 0) {
fprintf(stderr, "invalid number\n");
return 1;
}
ar = malloc(sizeof(*ar) * N);
if (ar == NULL) {
fprintf(stderr, "cannot allocate array for %d items\n", N);
return 1;
}
for (i = 0; i < N; i++) {
if (scanf("%d", &ar[i]) != 1) {
fprintf(stderr, "invalid or missing number for entry %d\n", i);
return 1;
}
}
sum = 0;
for (i = 0; i < N; i++) {
sum += ar[i];
}
printf("%d\n", sum);
free(ar);
return 0;
}
Finally, there is still a possibility for undefined behavior if the sum of the numbers exceeds the range of type int. Very few programmers care to detect such errors, but it can be done this way:
#include <limits.h>
...
sum = 0;
for (i = 0; i < N; i++) {
if ((sum >= 0 && arr[i] > INT_MAX - sum)
|| (sum < 0 && arr[i] < INT_MIN - sum)) {
fprintf(stderr, "integer overflow for entry %d\n", i);
return 1;
}
sum += ar[i];
}
#include <math.h>
#include <stdio.h>
int main()
{
int i=0,N,sum=0;
scanf("%d" ,&N);
int ar[N];
for(i=0; i<N; i++)
scanf("%d",&ar[i]);
for(i=0; i<N; i++)
sum=sum+ar[i];
printf("%d\n", sum);
return 0;
}
This should be the code.
You have initially declared the array of size 0 (because i=0).
Even though you declared the array of size 0, when I ran it on my machine, it actually executed successfully with the correct output.
This is generally due to undefined behavior which means that we can only guess the output when the code is correct. If the code has undefined behavior, then it can do whatever it wants (and in the worst case the code will execute successfully giving the impression that it's actually correct).
Declaring a Variable Size Array (VLA) is optional in C11 standard. Thus, it depends on the implementation of the compiler whether it will support VLA or not. As pointed out by #DavidBowling in comments, if the compiler does support, then declaring a VLA of size 0 can invoke undefined behavior (which you should avoid in all cases). If it doesn't support, then this will simply give a compilation error and you'll have to declare the array size as some integer constant (example, int arr[100];).
#include <math.h>
#include <stdio.h>
int main()
{
int i=0,N,sum=0;
scanf("%d" ,&N);
int ar[N];
for(i=0; i<N; i++)
{
scanf("%d",&ar[i]);
}
for(i=0; i<N; i++)
{
sum=sum+ar[i];
}
printf("%d\n", sum);
return 0;
}
You should declare the array after accepting the value of N.
#include <math.h>
#include <stdio.h>
int main()
{
int i=0,N,sum=0;
scanf("%d" ,&N);
int ar[N];
for(i=0; i<N; i++)
{
scanf("%d",&ar[i]);
sum=sum+ar[i];
}
printf("%d\n", sum);
return 0;
}
As this is a very simple question I'll expand it a bit to include some good programming practices.
1. Analyze the problem
We have to complete two tasks here:
Read and store the numbers to array.
Sum the array elements.
Of course you can both read and calculate the same time, but we ❤ the SoC design principle. This will help you later with bigger programs.
2. Create the program structure
In this state we have to consider what function to use, as we already solved the data structure problem (we use array).
Of course, we always can put the whole procedure in main function but this would break the SoC principle.
The main principle here is:
I create a function for a separate procedure.
So we'll have to build two functions. Let's consider the following example:
ReadArrayData will be used to read the data from the standard input (your keyboard in other words) to array. But what will declare as parameters? We surely have to pass the array and the array size. The return type of this function will be void (we don't have to return something).
Keep in mind that if you pass array to function you can manipulate it as you please and keep these changes in your main program. This is because the arrays are passed always by reference to a function.
In the end this will be your function prototype:
void ReadArrayData(int arraySize, int array[]);
CalculateArraySum will be used to calculate the sum of the array elements. The function prototype will be the same as for ReadArrayData with the difference that the returning type will be int (we return the sum).
int CalculateArraySum(int arraySize, int array[]);
3. Write your program
#include <stdio.h>
void ReadArrayData(int arraySize, int array[]);
int CalculateArraySum(int arraySize, int array[]);
int main(void) {
int N;
printf("Give the array size: ");
scanf("%d", &N);
int array[N];
ReadArrayData(N, array);
int sumOfArrayElements = CalculateArraySum(N, array);
printf("The sum of array elements is %d.\n", sumOfArrayElements);
return 0;
}
void ReadArrayData(int arraySize, int array[]) {
printf("Give %d elements: ", arraySize);
for (int i = 0; i < arraySize; ++i) {
scanf("%d", &array[i]);
}
}
int CalculateArraySum(int arraySize, int array[]) {
int sum = 0;
for (int i = 0; i < arraySize; ++i) {
sum += array[i];
}
return sum;
}
I know this was a large scaled answer, but I saw you are new to computer programing. I just wanted to present you the main functionality to solve all kinds of problems. This was just a small introduction. In the end, you have to remember what steps we take to solve a problem. With time and as you solve many problems you will learn many many other things.

Program for 1-D Statistics

So I'm having some issues with a program I'm writing to calculate statistics of an arbitrary amount of numbers. As you probably know, C does not have a native way of growing an array based on user input, so I made one. I'm not too familiar with pointers as I have not learned them yet. However, the main issue I'm having is that my program will not allow me to input anymore than 16 numbers. Any ideas on why?
#include <stdio.h>
#include <math.h>
float getMin(float arr[], int size)
{
int i;
float minimum = arr[0];
for (i=1; i < size; i++) {
if (arr[i] < minimum) {
minimum = arr[i];
}
}
return(minimum);
}
float getMax(float arr[], int size)
{
int i;
float maximum;
for(i=0; i<size; i++) {
if (arr[i] > maximum) {
maximum = arr[i];
}
}
return(maximum);
}
float getAverage(float arr[],int size)
{
int i;
float avg, sum;
for(i=0; i<size; i++) {
sum += arr[i];
}
avg = sum/size;
return avg;
}
float getVar(float arr[], int size, float average)
{
int i;
float var, diff[size], sq_diff[size], sum;
for(i=0; i<size; i++) {
diff[i] = arr[i] - average;
}
for(i=0; i<size; i++)
{
sq_diff[i] = pow(diff[i], 2);
}
var=getAverage(sq_diff, size);
return(var);
}
float getStdDev(float var)
{
float std_dev = sqrt(var);
return(std_dev);
}
int main(void)
{
int n=0, i=0;
float array[n], x;
printf("This program computes 1-D statistics.");
printf("\nEnter a negative number to stop input.\n");
do {
printf("Number %d: ", i+1);
scanf("%f", &x);
n++;
array[i] = x;
i++;
}while(x >= 0);
float avg = getAverage(array, n-1);
float var = getVar(array, n-1, avg);
printf("\nMinimum: %f", getMin(array, n-1));
printf("\nMaximum: %f", getMax(array, n));
printf("\nAverage: %f", getAverage(array, n-1));
printf("\nVariance: %f", getVar(array, n-1, avg));
printf("\nStandard Deviation: %f", getStdDev(var));
return(0);
}
I am afraid you have some general miss-understanding of how C and programming languages work in general. Programming is not exactly the same as math. If you declare a variable n and an array myarr[n], your array won't grow as n grows. The array will take the value n had at the point you declared it and will use that. Also declaring arrays like that is probably not a good idea, I don't think many C compilers will compile what one would expect. In your case you are declaring an array of size 0, the fact that it seems to work up to 16 numbers is completely system dependent. You can not expect this kind of behavior to be consistent over different systems/compilers.
Instead what you should do if you really need this is use dynamic memory. I have written a simple example really fast you could use. This is by no means a complete but for your needs it should be good enough. Use the initArr(&arr) whenever you create a new growing array. If for example you have an array of type MyArr with name arr you can get the data of the array like this arr.data[i] while you can get the length of the array (the meaningful data) like this arr.len. Finally you can get the current max size of your array like this arr.size. Please not that you should only add new elements using addElement(&arr, x) as this function will take care of the array growth. When you are done using your array you can use destrArr(&arr) to free the memory. This is good practice even though when your program exits the memory is freed anyway.
Finally please review your functions. I haven't been through your logic but remember that you should never exceed boundaries. For example you should not try to access array[-1] or array[size_of_array] where size_of_array is the total size of your array. Remember that an array declared as float arr[100] can only safely be used in the region arr[0] to arr[99]. Also doing sum += x is equivalent to doing sum = sum + x. Consider what the initial value of sum is if you do not initialize it. In fact it depends but could contain garbage values.
Try looking at your compiler warnings and fix them. It is worth understanding them and are rarely worth neglecting. Try the -Wall flag if you are using gcc, it might give you some more warnings worth looking at.
I hope this was helpful and not too confusing. In any case have a look at the following code and add your functions and libraries back. This should work out of the box.
Good Luck! :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INIT_SIZE 2
typedef struct MyArr {
float *data;
int size;
int len;
} MyArr;
int initArr(MyArr *arr) {
arr->data = (float *)malloc(INIT_SIZE*sizeof(float));
if (arr->data == NULL) {
printf("OUT OF MEMORY\n");
return -1;
}
arr->size = INIT_SIZE;
arr->len = 0;
return 1;
}
int addElement(MyArr *arr, float x) {
if (arr->len == arr->size) {
int i;
float *tmp = (float *)malloc(2*arr->size);
if (tmp == NULL) {
printf("OUT OF MEMORY\n");
return -1;
}
for (i = 0; i < arr->size; ++i) {
tmp[i] = arr->data[i];
}
free(arr->data);
arr->data = tmp;
arr->size = 2*arr->size;
}
arr->data[arr->len] = x;
arr->len++;
return 1;
}
void destrArr(MyArr *arr) {
free(arr->data);
arr->size = 0;
arr->len = 0;
}
int main(void)
{
int n=0, i=0;
float x;
MyArr array;
initArr(&array);
printf("This program computes 1-D statistics.");
printf("\nEnter a negative number to stop input.\n");
do {
printf("Number %d: ", i+1);
scanf("%f", &x);
n++;
addElement(&array, x);
i++;
}while(x >= 0);
float avg = getAverage(array.data, n-1);
float var = getVar(array.data, n-1, avg);
printf("\nMinimum: %f", getMin(array.data, n-1));
printf("\nMaximum: %f", getMax(array.data, n));
printf("\nAverage: %f", getAverage(array.data, n-1));
printf("\nVariance: %f", getVar(array.data, n-1, avg));
printf("\nStandard Deviation: %f", getStdDev(var));
destrArr(&array);
return(0);
}

Radix Sorting Algorithm in c [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have been tried to write radix sort in c.when i run my code with the static array it works well. but when i am trying to take random inputs from file it gives me an "Segmentation fault" at run time.help Please just help to modify this code
here is my code:
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<string.h>
int getMax(int arr[], int n)
{
int mx = arr[0];
int i;
for (i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
void countSort(int arr[], int n, int exp,int base)
{
int output[n];
int i;
int count[base];
memset(count, 0, sizeof count);
for (i = 0; i < n; i++)
count[ (arr[i]/exp)%base]++;
for (i = 1; i < base; i++)
count[i] += count[i - 1];
for (i = n - 1; i >= 0; i--)
{
output[count[ (arr[i]/exp)%base ] - 1] = arr[i];
count[ (arr[i]/exp)%base ]--;
}
for (i = 0; i < n; i++)
arr[i] = output[i];
}
void radixsort(int arr[], int n,int base)
{
int m = getMax(arr, n);
int exp;
for (exp = 1; m/exp > 0; exp *= 10)
countSort(arr, n, exp , base);
}
void print(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ",arr[i]);
}
int main(int argc,int argv[])
{
int base=atoi(argv[1]);
int num,i;
FILE *fp1=fopen("myFile1.txt","r");
int arr[50];
while(fscanf(fp1,"%d",&num)==1)
{
arr[i]=num;
i++;
}
int n = sizeof(arr)/sizeof(arr[0]);
radixsort(arr, n ,base);
print(arr, n);
fclose(fp1);
return 0;
}
You are assuming that the compiler has set the initial value of i to 0. However, this is not guaranteed. While many compilers reset variables to 0, many others just leave the contents of memory set at whatever it happened to be at compile time or at load time. You need to initialize the value before using it.
Additionally, you do not test to ensure that you are not overrunning the arr buffer. For example consider what would happen to arr[] if you happen to open a file that has 51 entries. You would attempt to add an entry to arr[50] which overruns the buffer.
You need to initialize i to 0 and make sure to break out if i becomes too great.
The calculation of n is always 50 because arr is 50 ints. You should use i as the count of how many entries have been read in.
int main(int argc,int argv[])
{
int base=atoi(argv[1]);
// int num,i; // This is the line that causes the error
int num;
int i = 0; // This needs to be initialized before use.
FILE *fp1=fopen("myFile1.txt","r");
int arr[50];
// You need to ensure that i does not overrrun the buffer.
while(fscanf(fp1,"%d",&num)==1 && (i < 49))
{
arr[i]=num;
i++;
}
// Since i was defined before the while, it should have the correct count
// This calculation of n is wrong if fewer than a full buffer is read
int n = sizeof(arr)/sizeof(arr[0]);
radixsort(arr, n, base);
print(arr, n);
fclose(fp1);
return 0;
}

Random number generator generating the wrong set of numbers

I'm attempting to make a program that generates an array of random numbers where no two cells contain the same number within a given range.
Example: asking it to make an array of 4 should yield something like: 4 2 1 3, instead what I get is this: 4 2 1360726912 245694014
#include <stdio.h>
#include <time.h>
#define TRUE 1
#define FALSE 0
// generates a random number within a given range
int random(int from, int to) {
int
high = to + 1,
low = from;
return (rand() % high - low) + low;
}
// returns a random number different from any number in the array
int gen_different(int arr_len, int arr[]) {
int rand_val = random(1, arr_len);
int matches = FALSE;
for(int i = 0; i < arr_len; i++) {
if(rand_val == arr[i]) matches = TRUE;
}
if(matches) gen_different(arr_len, arr);
return rand_val;
}
// generates an array with a given number of cells, containing all different numbers
void gen_arr(int count, int arr[]) {
for(int i = 0; i < count; i++)
arr[i] = gen_different(count, arr);
}
int main() {
srand(time(NULL));
printf("Please enter an array number: ");
int num = 0;
scanf("%d", &num);
int* arr[num];
gen_arr(num, arr);
for(int i = 0; i < num; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
I get that my issue has something to do with pointers, but I'm not quite sure what I should change to make it work. Any help?
you are initializing arr[num] as
int* arr[num]; //array of pointers to integers
it should be like
int arr[num]; ////array of integers
One possible problem is you are declaring your array as a pointer to an array
int* arr[num];
this is the same as:
int ** arr;
You should actually use
int arr[num];
As you are expecting an array in your functions.
I think that:
int* arr[num];
is not what you want. Try
int arr[num];
(Note this is not legal C before C99)
Expression:
(rand() % high - low) + low
looks suspect as it is the same as rand() % high.
Did you mean:
(rand() % (high - low)) + low
?

Resources