My program is looping infinitely, and I'm honestly not sure why - c

So the problem here was to write a program that uses pointers to point to functions and write it in such a way that it collects 10 doubles, gives feedback to the user of the program, sorts them and print the sorted results as proof. The problem is, the program either prints the printf statement in the beginning infinitely, or collects numbers infinitely.
Here is some code
#include <stdio.h>
void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
void sort(double *p[], int n);
void print_doubles(double *p[], int n);
int main(void){
double *numbers[9];
int nbr;
printf("\nEnter 10 doubles that are less than 5 or greater than 5, type 0 to exit");
for(int i = 0; i < 10 ; i++)
{
scanf("%d", &nbr);
func1(nbr);
numbers[i] = nbr;
if(nbr == 0)
break;
}
sort(numbers, 10);
print_doubles(numbers, 10);
return 0;
}
void func1(double val)
{
double (*ptr)(void);
if(val <= 5.00){
ptr = below_five;
}else if((val > 5.00) && (val <= 10.00)){
ptr = above_five;
}else
ptr = other;
}
void below_five(void){
puts("You entered a number below or equal to five");
}
void above_five(void){
puts("You entered a number above five");
}
void other(void){
puts("You entered a number well above five.");
}
void sort(double *p[], int n)
{
double *tmp;
for(int i = 0; i < n; i++)
{
if(p[i] > p[i+1]){
tmp = p[i];
p[i] = p[i+1];
p[i + 1] = tmp;
}
}
}
void print_doubles(double *p[], int n)
{
int count;
for(count = 0; count < n; count++)
printf("%d\n", p[count]);
}
Like I said, what I expect it to be able to do is collect doubles into the scanf method and then print the numbers after sorting them, but it seems the for loop collects doubles forever without end in this case.
What have I done wrong, exactly?

See my comments in your updated code.
There are other modifications required, but the minimum updates needed to work your code is below
#include <stdio.h>
void func1(double x);
void below_five(void);
void above_five(void);
void other(void);
void sort(double p[], int n); /* Simply use a array notation,
arrays passed to functions decays to pointer */
void print_doubles(double p[], int n); /* Simply use a array notation,
arrays passed to functions decays to pointer */
int main(void){
double numbers[10];
double nbr; // Change type to double, as you're reading doubles
printf("\nEnter 10 doubles that are less than
5 or greater than 5, type 0 to exit\n");
for(int i = 0; i < 10 ; i++)
{
scanf("%lf", &nbr); // Use correct format specifier to read doubles
func1(nbr);
numbers[i] = nbr;
if(nbr == 0)
break;
}
sort(numbers, 10);
print_doubles(numbers, 10);
return 0;
}
void func1(double val)
{
double (*ptr)(void);
if(val <= 5.00){
ptr = below_five;
}else if((val > 5.00) && (val <= 10.00)){
ptr = above_five;
}else
ptr = other;
/* Why you set the pointer to function if you don't call it,
so call it here*/
(*ptr)();
}
void below_five(void){
puts("You entered a number below or equal to five");
}
void above_five(void){
puts("You entered a number above five");
}
void other(void){
puts("You entered a number well above five.");
}
void sort(double p[], int n) /* Your sorting routine is wrong ,
see the modified code */
{
double tmp;
for(int j = 0; j < n-1; j++)
for(int i = 0; i < n-j-1; i++)
{
if(p[i] > p[i+1]){
tmp = p[i];
p[i] = p[i+1];
p[i + 1] = tmp;
}
}
}
void print_doubles(double p[], int n)
{
int count;
for(count = 0; count < n; count++)
printf("%lf\n", p[count]); // Use correct format specifier
}
Demo Here

Related

Sorting unique vectors in c

The program should eliminate any repeating digits and sort the remaining ones in ascending order. I know how to print unique digits but I donĀ“t know how to create a new vector from them that i can later sort.
#include <stdio.h>
void unique(double arr[], int n) {
int i, j, k;
int ctr = 0;
for (i = 0; i < n; i++) {
printf("element - %d : ",i);
scanf("%lf", &arr[i]);
}
for (i = 0; i < n; i++) {
ctr = 0;
for (j = 0, k = n; j < k + 1; j++) {
if (i != j) {
if (arr[i] == arr[j]) {
ctr++;
}
}
}
if (ctr == 0) {
printf("%f ",arr[i]);
}
}
}
int main() {
double arr[100];
int n;
printf("Input the number of elements to be stored in the array: ");
scanf("%d", &n);
unique(arr, n);
}
You can always break a larger problem down into smaller parts.
First create a function that checks if a value already exists in an array.
Then create a function that fills your array with values. Check if the value is in the array before adding it. If it is, you skip it.
Then create a function that sorts an array. Alternatively, qsort is a library function commonly used to sort arrays.
This is far from efficient, but should be fairly easy to understand:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NUMS 256
int find(double *arr, size_t length, double val)
{
for (size_t i = 0; i < length; i++)
if (val == arr[i])
return 1;
return 0;
}
size_t fill_with_uniques(double *arr, size_t limit)
{
size_t n = 0;
size_t len = 0;
while (n < limit) {
double value;
printf("Enter value #%zu: ", n + 1);
if (1 != scanf("%lf", &value))
exit(EXIT_FAILURE);
/* if value is not already in the array, add it */
if (!find(arr, len, value))
arr[len++] = value;
n++;
}
return len;
}
int compare(const void *va, const void *vb)
{
double a = *(const double *) va;
double b = *(const double *) vb;
return (a > b) - (a < b);
}
int main(void)
{
double array[MAX_NUMS];
size_t count;
printf("Input the number of elements to be stored in the array: ");
if (1 != scanf("%zu", &count))
exit(EXIT_FAILURE);
if (count > MAX_NUMS)
count = MAX_NUMS;
size_t length = fill_with_uniques(array, count);
/* sort the array */
qsort(array, length, sizeof *array, compare);
/* print the array */
printf("[ ");
for (size_t i = 0; i < length; i++)
printf("%.1f ", array[i]);
printf("]\n");
}
Above we read values from stdin. Alternatively, fill_with_uniques could take two arrays, a source and a destination, and copy values from the former into the latter, only when they would be unique.
Remember to never ignore the return value of scanf, which is the number of successful conversions that occurred (in other words, variables assigned values). Otherwise, if the user enters something unexpected, your program may operate on indeterminate values.

The fastest and most efficient way to find the number of distinct elements of a 1D array

So I'm very new to programming and the C language, and I would like to find the simplest, fastest, and most efficient way to count all the distinct elements of a 1D array. This was actually for a school assignment, but I've been stuck on this problem for days, since my program was apparently too slow for the online judge and it got a TLE. I've used regular arrays and dynamically allocated arrays using malloc, but neither worked.
Anyways, here's the latest code of it(using malloc):
#include <stdio.h>
#include <stdlib.h>
int distinct(int *arr, int N){
int j, k, count = 1;
for(j = 1; j < N; j++){
for(k = 0; k < j; k++){
if(arr[j] == arr[k]){
break;
}
}
if(j == k){
count++;
}
}
return count;
}
int main(){
int T, N, i = 0;
scanf("%d", &T);
do{
scanf("%d", &N);
int *arr;
arr = (int*)malloc(N * sizeof(int));
for(int j = 0; j < N; j++){
scanf("%d", &arr[j]);
}
int count = distinct(arr, N);
printf("Case #%d: %d\n", i + 1, count);
i++;
}while(i < T);
return 0;
}
The most efficient way depends on too many unknown factors. One way is to sort the array and then to count distinct elements in there, skipping the duplicates as you go. If you have sorted the array and gotten this:
1 1 1 1 2 2 2 2 3 3
^ ^ ^
+-skip--+-skip--+-- end
... you can easily see that there are 3 distinct values in there.
If you don't have a favourite sorting algorithm handy, you could use the built-in qsort function:
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
Example:
#include <stdio.h>
#include <stdlib.h>
int compar(const void *l, const void *r) {
const int* lhs = l;
const int* rhs = r;
if(*lhs < *rhs) return -1; // left side is less than right side: -1
if(*lhs > *rhs) return 1; // left side is greater than right side: 1
return 0; // they are equal: 0
}
int distinct(int arr[], int N){
// sort the numbers
qsort(arr, N, sizeof *arr, compar);
int count = 0;
for(int i=0; i < N; ++count) {
int curr = arr[i];
// skip all numbers equal to curr as shown in the graph above:
for(++i; i < N; ++i) {
if(arr[i] != curr) break;
}
}
return count;
}
int main() {
int T, N, i = 0;
if(scanf("%d", &T) != 1) return 1; // check for errors
while(T-- > 0) {
if(scanf("%d", &N) != 1) return 1;
int *arr = malloc(N * sizeof *arr);
if(arr == NULL) return 1; // check for errors
for(int j = 0; j < N; j++){
if(scanf("%d", &arr[j]) != 1) return 1;
}
int count = distinct(arr, N);
free(arr); // free after use
printf("Case #%d: %d\n", ++i, count);
}
}

Trying to write a program to fill an array with random numbers in C

My issue is that I am getting segmentation fault (core dumped) each time I try, I have yet to clean up my code, but I am stumped.
I must enter the values in with the compiler e.g "./filename 0 100" whereby 0 is min and 100 is max.
It must then fill the array of 10 elements with random numbers (0-100). I am so close, just can't fathom the main function.
Also, how can I print the array {0,1,2,3} in format "[0,1,2,3]" including the commas, without it looking like "[0,1,2,3, ]"
#include <stdlib.h>
#include <stdio.h>
int getRandom(int min, int max);
void fillArray(int data[], int size, int min, int max);
void printArray(int data[], int size);
int main(int argc, char *argv[]) {
int a;
int b;
if (argc>=3){
a = atoi(argv[1]);
b = atoi(argv[2]);
int arr[10];
printf("\t An array with random values from 0 to 100 \n");
fillArray(arr,10 ,a, b);
printArray(arr, 10);
} else {
printf("Incorrect number of arguments - please call with assignment min max\n");
}
return 0;
}
int getRandom(int min, int max) {
int result = 0;
int low = 0;
int high = 0;
if (min<max) {
low = min;
high = max+1;
} else {
low = max + 1;
high = min;
}
result = (rand() % (high-low)) + low;
return result;
}
void fillArray(int data[], int size, int min, int max){
int i;
for(i=min ; i < max+1; i++){
data[i] = getRandom(min,max);
}
}
void printArray(int data[], int size){
int i;
printf("[");
for(i=0; i<size; i++){
printf("%d,", data[i]);
}
printf("]");
}
I agree with #Steve Friedl that the main problem with your program lies in the fillArray function. There i should run from 0 to size.
As for your second question, testing whether you're printing the last number helps to suppress the unwanted comma:
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
printf("%d", data[i]);
if (i < size - 1)
printf(",");
}
printf("]");
}
If you prefer a more compact solution (although with an optimizing compiler there's not really a difference), you could write it as:
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
printf("%d%c", data[i], i < size-1 ? ',' : ']');
}
}
Also, in your main function, you should include a and b in your printing:
printf("\t An array with random values from %d to %d \n", a, b);
I believe this is blowing things up for you:
void fillArray(int data[], int size, int min, int max){
int i;
for(i=min ; i < max+1; i++){ // <-- HERE
data[i] = getRandom(min,max);
}
}
The calling function allocates 10 items in the arr array, and that's passed as the size parameter, but you're not using that parameter to limit filling up the array. If the max value is 100, then it's trying to fill one hundred slots instead of just ten.
for (i = 0; i < size; i++)
data[i] = getRandom(min,max);
should fix at least this issue.
EDIT: The comma thing, I prefer to add commas before the items unless this is the first. In this case it doesn't matter much, but it's more general, especially for variable-length lists where you don't know you're at the end until you get there. Augmenting the helpful response from #JohanC :
void printArray(int data[], int size) {
printf("[");
for (int i = 0; i < size; i++) {
if (i > 0) printf(",");
printf("%d", data[i]);
}
printf("]");
}

How to ignore/remove leading zeros?

I am writing a program to add two large numbers in C.
My integer array result holds the sum of the two numbers (which were also stored in arrays).
For example, if the result array is [0,0,3,2] (actual array size is 20)
If 32 is my actual result, how can I display the contents of the result array without the leading zeros ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE 10
void align(int A[],int n);
void add(int A[],int B[], int C[]);
void Invert(int* a, int n);
int main(int argc, char** argv){
char input1[20];
char input2[20];
int size = 20;
int a;
int b;
int num1[20];
int num2[20];
int result[20];
int length1 = strlen(argv[1]);
int length2 = strlen(argv[2]);
int i = 0;
for (i=0;i<length1;i++){
input1[i] = argv[1][i];
}
for (i=0;i<length2;i++){
input2[i] = argv[2][i];
}
a=atoi(input1);
b=atoi(input2);
align(num1,a);
align(num2,b);
add(num1,num2,result);
Invert(result,size);
for (i=0;i<20;i++){
printf("%d",result[i]);
}
return 0;
}
void align (int A[], int n){
int i = 0;
while (n) {
A[i++] = n % BASE;
n /= BASE;
}
while (i < 20) A[i++] = 0;
}
void add (int A[], int B[], int C[]) {
int i, carry, sum;
carry = 0;
for (i=0; i<20; i++) {
sum = A[i] + B[i] + carry;
if (sum >= BASE) {
carry = 1;
sum -= BASE;
} else
carry = 0;
C[i] = sum;
}
if (carry) printf ("overflow in addition!\n");
}
void Invert(int* a, int n)
{
int i;
int b;
for(i=0; i<n/2; i++){
b = a[i];
a[i] = a[n-i-1];
a[n-i-1] = b;
}
}
`
To get the actual digits (I assume that each digit is stored as a byte in an array of 20 bytes, lowest digit at highest index), you do something like this:
int i;
int size = sizeof(thearray) / sizeof(thearray[0]);
/* find first non-0 byte, starting at the highest "digit" */
for (i = 0; i < size - 1; ++i)
if (thearray[i] != 0)
break;
/* output every byte as character */
for (; i < size; i++)
printf("%c", thearray[i] + '0'); /* 0 --> '0', 1 --> '1', etc. */
printf("\n");
You can do this by below code:-
int flag=1;
for(i=0;i<20;i++)
{
if(flag==1&&array[i]!=0)
flag=0;
if(flag!=1)
{
printf("%d",array[i]);
}
}
This will remove all leading zeros.
I propose a solution by using the pointer. The situation where only zero is stored in the array is also handled. I'm more comfortable with the pointer.
int test[20] = {0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,1,2,3,4,5};
int test_bis[20] = {0};
int * ptr_test = test_bis;
int ii = 0;
while( *(ptr_test)== 0 && ii < 20 ) {
ptr_test++;
ii++;
}
if( ii < 20)
do {
printf("%d",*(ptr_test));
ptr_test++;
} while (++ii < 20);
else
printf("0");
Thats for integer array you can modify it accordingly.
for(i=0;i<20;i++){
if(flag==1&&array[i]==0)
{
// just skips until first nonzero
}
else if(flag==1&&array[i]!=0){
flag=0; // when first nonzero comes set flag to 0 and print it
printf("%d",array[i]);
}
else {
printf("%d",array[i]); // after first nonzero simply print it
}
}

Generating an array of integers

I got a problem in generating an array of integers. the program fills hundreds of cells of the array with numbers like -32429173 instead of 3 cells with numbers from 0 to 3 (for example). Maybe the problem is in the wrong way of allocating memory? Here is the wrong part of the code. Thx for the help in advance.
int* generate()
{
int maxnumb;
int i;
scanf_s("%d",&size); //size of an array
scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
int* array=(int*)calloc(size,sizeof(int));
for (i=0;i<size;i++)
array[i] = rand() % maxnumb + 1;
return array;
}
Here is the full code
#define _CRT_SECURE_NO_WARNINGS
#include<stdlib.h>
#include<stdio.h>
int size;
void swap(int* elem1,int* elem2) //swap elements
{
int temp;
temp=*elem1;
*elem1=*elem2;
*elem2=temp;
}
void bublesort(int* array,int size) //bublesort
{
for (int j=1;j<size-1;++j)
{
for (int i=0;i<size-j;++i)
{
if ((array[i])>(array[i+1]))
swap(&array[i],&array[i+1]);
}
}
}
int* enterHand() //handle entering
{
int i;
scanf_s("%d",&size);
int* array=(int*)calloc(size,sizeof(int));
for (i=0;i<size;i++)
{
scanf_s("%d",&array[i]);
}
return array;
}
int* enterFile() //entering from the file
{
int i;
int singlenumb;
FILE* foo;
errno_t err;
err=fopen_s(&foo,"input.txt","r");
if( err == 0 )
{
printf( "The file 'input.txt' was opened\n" );
}
else
{
printf( "The file 'input.txt' was not opened\n" );
}
while (!feof(foo))
{
fscanf_s(foo, "%d", &singlenumb);
size++;
}
size-=1;
int* array=(int*)calloc(size,sizeof(int));
rewind(foo);
i=0;
while (i!=size)
{
fscanf_s(foo, "%d", &array[i]);
i++;
}
fclose(foo);
return array;
}
int* generate()
{
int maxnumb;
int i;
scanf_s("%d",&size); //size of an array
scanf_s("%d",&maxnumb); //asks for maxnumb to fill the array with random numbers from 0 to maxnumb
int* array=(int*)calloc(size,sizeof(int));
for (i=0;i<size;i++)
array[i] = rand() % maxnumb + 1;
return array;
}
void putsFile(int* array, int size)
{
int i=0;
int k;
FILE* fooo;
fopen_s(&fooo,"output.txt","w");
while (i!=size)
{
for (k=0; k<10; k++)
{
fprintf(fooo,"%d ", array[i]);
i++;
}
fprintf(fooo,"\n");
}
fclose(fooo);
}
void printArray(int* array, int size)
{
int i=0;
int k;
while (i!=size)
{
for (k=0; k<10; k++)
{
printf("%d ", array[i]);
i++;
}
printf("\n");
}
}
int main()
{
int choice;
int* pntr;
printf("choose a type of filling an array\n1 = handle filling\n2 = filling from the file\n3 = generating\nenter the number...\n");
scanf("%d", &choice);
switch (choice)
{
case 1: {pntr=enterHand();} break;
case 2: {pntr=enterFile();} break;
case 3: {pntr=generate();} break;
default: {pntr=NULL;}
}
bublesort(pntr,size);
printf("choose a type of typing an array\n1 = console\n2 = file\nenter the number...\n");
scanf("%d", choice);
switch (choice)
{
case 1: {printArray(pntr, size);} break;
case 2: {putsFile(pntr, size);} break;
default: {printf("you entered the wrong number");}
}
return 0;
}
I think you should initialize your maxnumb
First, here is a simpler approach for filling an array.
int randomGenerator(int min, int max);
int main(void)
{
int array[100]//or any size
for(i=0;i<sizeof(array)/sizeof(array[0]);i++)
{
array[i]=randomGenerator(1,3);//will issue only 1, 2 or three (as you requested)
} //change min and max for wider range
return 0;
}
int randomGenerator(int min, int max) //set the range of desired numbers in array
{
int random=0, trying=0;
trying = 1;
while(trying)
{
srand(clock());
random = (rand()/32767.0)*(max+1);
(random >= min) ? (trying = 0) : (trying = 1);
}
return random;
}
There are also some things you can do to make this non-repeating, i.e. so you won't get two Ace of Spades. But for now, you have much bigger issues...
Your code, as is has too many issues to build. If you post a small, buildable section, with the specific problem, it can be better addressed.
The printArray() and putsFile() may print outside array size.
int* enterFile() does not set size to 0 before determining number of int in the file, thus using a potentially unexpected non-zero size.
[Edit] Well size is implicitly set to 0 being in the global uninitialized space. Still, recommend explicitly setting to 0 in enterFile().
#if 0
// The value of size is dependent on program history or uninitialized.
while (!feof(foo)) {
fscanf_s(foo, "%d", &singlenumb);
size++;
}
size-=1;
#else
size = 0; // Reset size to 0
while (1 == fscanf_s(foo, "%d", &singlenumb)) {
size++;
}
#endif
Suggest:
void printArray(int* array, int size) {
// Also in void putsFile(int* array, int size)
int i=0;
while (i < size) {
int k;
for (k=0; k<10; k++) {
printf("%d ", array[i]);
i++;
// Add
if (i >= size) break;
}
printf("\n");
}
}

Resources