It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Does anyone know how to change the code below from enthusiasticgeek to allow for user input (e.g. Input n, and then n integers)? Output these n integers and the median. n is odd and positive and is less than 1 million.
Code pasted from enthusiasticgeek.com:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 6
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, ELEMENTS, sizeof(int), compare);
for (n=0; n<ELEMENTS; n++)
{ printf ("%d ",values[n]); }
printf ("median=%d ",values[ELEMENTS/2]);
return 0;
}
use:
int values[static or dynamic allocation dependent]
for (i=0;i<n;i++){
printf("Enter number:");
scanf(%d, value[i]);
}
instead of just hardcoding the values for the values array.
Make sure you either dynamically declare the array, or statically make it large enough to accept the number of integers you want, 1,000,000 but thats a ton of memory. No one's going to take that much time to enter that many numbers manually. Probably safe with declaring it at 30 tops.
Or if you're planning to use the "define elements" part, you only need to declare the values array to be 6.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I need to Write a function in C language whose output with respect to input should like this:
The above table is just for an example. The input is not limited to 25, and also the number of inputs in a particular range is X instead of 5. I cannot figure out how to do this?
Right now I don't have enough time write a better question ;). Please edit it if you found any mistake.
int f(int x, int X){
return (x + (X-1))/X;
}
int func(int x)
{
if(x%5 == 0)
return x/5;
else
return x/5 + 1;
}
What about an array of structs along
struct range {
int lo, hi, result;
}
Ask the user for X, then allocate an array with X instances of this struct,
#include <stdlib.h>
struct range *array = malloc (X * sizeof *array);
Now loop over X table rows asking for the lo, hi and result. The rest is left as an exercise...
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
suppose i have a two D array.
#define ROWS 3
#define COLS 3
char a[ROWS][COLS]= {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'}
};
How can I copy values of that to a single array. I want only COLS values.
1) If you need to copy the array in row wise order
you could use :
-first have a 1D array that could hold all the elements of 2D array, then use
memcpy(new1Darr, org2Darr, total size in bytes);
like for above example,
memcpy(new1Darray, a, sizeof(char)*ROWS*COLS)
2) Instead of (1) or if you want to change the order in which the data must be stored then just traverse through the 2D array the way you want(column major) and copy the elements one by one. Like (considering you define all the variables first)
This will copy the elements in the new array in column wise order
k=0;
for(j=0;j<COLS;j++)
{
for(i=0;i<ROWS;i++)
{
new1Darray[k] = a[i][j];
k++;
}
}
Its very simple. Lets see 'how?'-
#define ROWS 3
#define COLS 3
#include<stdio.h>
#include<conio.h>
char a[ROWS*COLS]={'1','2','3','4','5','6','7','8','9'};
void main()
{
for(int i=0; i<ROWS*COLS;i=i+COLS)
{
for(int j=0; j<COLS; j++)
{
printf("%c\t",a[i+j]); //access array
}
printf("\n");
}
getch();
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
My order is defined as 'A < a < B < b ...< Z < z'.
I have to find if a given string is in range or not.
Ex. If my range is AaA - BaB, AA or AaaB is inthe range, but not CbAA.
I am looking for any pointers, ideas, suggestions to help me start. I will implement this in C.
So all you need to implement is a single function that compares two strings according to your rules. It is kind of modified lexicograogical sorting:
int compare_letters(char x, char y) {
char lx = tolower(x);
char ly = tolower(y);
if (lx != ly) {
return lx < ly;
} else {
return x < y;
}
}
int smaller(const char* a, const char* b) {
.. use the above function ...
}
Now make use of the above function and to check if a given string x is in the range (a,b), check if smaller(a, x) and smaller(x, b). That's it.
Some tips on the function smaller - compare the strings char by char and if the two chars differ, return their compare_letter. If one of the strings runs out of letters, consider it smaller.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Basically i want to create a recursive function to implement this program in C
#include <stdio.h>
main()
{
float guess=1,num,num1;
int i;
printf("enter any number:\n");
scanf("%f",&num);
num1=num;
for (i=1;num1>1;i++,num1/=10); //to calculate no of digits in input
i=i/2;
printf("i:%d\n",i); //to make a better guess
for (;i>0;i--,guess*=10);
printf("guess = %f\n",guess);
for (i=1;i<=10;i++) //evaluate square root improving accuracy with each loop
{
guess=(guess+num/guess)/2;
}
printf("sqrt: %f\n",guess);
}
Something like this:
#include <math.h>
#include <float.h>
float MySqrt(float num, float prev)
{
float next = (prev+num/prev)/2;
if (fabs(next-prev)<FLT_EPSILON*next)
return next;
return MySqrt(num, next);
}
To call it, pass 1.0 as your initial guess for the prev parameter.
You can easily make this fail with a stack overflow by passing in bad data, but you probably aren't going to be tested on that in this assignment.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
Note: The question has been edited to make the problem go away.
I have written this code to reverse an array using functions. But there is an error in line 24 saying ' ) expected'. I have read it again and again but i couldn't find the error. Can anybody please reveal it and tell me how to remove it?
#include<stdio.h>
#include<conio.h>
#define max 5
/*function prototype*/
void reverse(int[],int);
void main()
{
int arr[max]={1,2,3,4,5};
int i,j;
clrscr();
printf("the list before reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
reverse(arr,max);
printf("\n the list after reversing:\n");
for(i=0;i<max;i++)
printf("%d",arr[i]);
getch();
}
/*function for reversing elements of array*/
void reverse(int num[],int max)
{
int i,j,temp;
for(i=0,j=max-1;i<max/2;i++,j--)
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
max is defined as a macro. So after preprocessing it becomes
void reverse(int num[],int 5)
Which is not valid and you are getting ' ) expected'. If max is constant then there is no need to pass it as a parameter. And also you have a missing for in the function.
You seem to be missing the for keyword in the loop header in reverse().
EDIT:
Ok, ok my first answer was stupid...
(Answered without thinking enough)
Now I got it:
The problem is
#define max 5
Later max (=5) is used as a parameter!