Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
The variable named "stop" does not add up when displayed. It should be displayed "Input numbers for array 1 then 2...5".
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#define MAX_SIZE 1000
void main(){
int num[MAX_SIZE];
printf("Input number of integers in the array: ");
scanf("%d", &num[MAX_SIZE]);
for(size_t stop=0; stop<num[MAX_SIZE]; stop++){
printf("\nInput numbers for array %d: ", num[stop]);
scanf("%d", &num[stop]);
}
}
the picture
You have two options.
Keep the preprocessor directive:
#include <stdlib.h>
#include <stdio.h>
#define MAX_SIZE 10
int main(){
int num[MAX_SIZE];
int i;
for(i=0; i < MAX_SIZE; i++){
printf("\nInput numbers for array %d: ", i);
scanf("%d", &num[i]);
}
return 0;
}
Or, which is most likely what you are trying to do, use dynamic memory allocation:
#include <stdlib.h>
#include <stdio.h>
int main(){
int *num;
int i, maxSize;
printf("Input number of integers in the array: ");
scanf("%d", &maxSize);
num = (int *)malloc(maxSize * sizeof(int)); // allocate dynamic memory
for (i=0; i < maxSize; i++){
printf("\nInput numbers for array %d: ", i);
scanf("%d", &num[i]);
}
free(num); // free pointer
return 0;
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", arr[i]);
}
display(&arr[0], n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}
This is the code for printing the array values using function. And in Run-time it gives Segmentation fault error. Help me to fix this.
You have to pass the address of the variable when calling scanf() in the loop, just as you do when reading n.
When passing an array to a function, we usually just write the name of the array. This is equivalent to passing &arr[0], but we don't normally write that out.
#include <stdio.h>
int display(int arr[], int n);
int main()
{
int i, n;
printf("\nEnter the Size of Array:");
scanf("%d", &n);
int arr[n];
printf("\nEnter the %d Values of Arrays", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
display(arr, n);
return 0;
}
int display(int arr[], int n)
{
int i;
printf("\nThe %d elements are:\n");
for (i = 0; i < n; i++)
{
printf("Array [%d]= %d\n", i, arr[i]);
}
}
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 5 years ago.
Improve this question
I wrote the following code to find the max number from a array of numbers. Apparently there is an error in my code.It is a segmentation error. Please help me identify it.
#include <stdio.h>
void max(int n,int A[n]);
int main()
{
int n;
int A[n];
max(n,A[n]);
}
void max(int n,int A[n])
{
printf("Enter the number of elements you want in your array\n");
scanf("%d",&n);
int i;
printf("Enter the elements in your array\n");
for(i=0;i<n;i++)
{
scanf("%d",&A[i]);
}
int max=A[0];
for(i=1;i<n;i++)
{
if(A[i]>max)
{
max=A[i];
}
}
printf("%d",max);
}
int n;
int A[n];
You have to initialize n otherwise it contains garbage value. And now after this point you run into undefined behavior.
Correct code would be
#include <stdio.h>
#include <stdlib.h>
void printMax(int n,int A[]);
int main()
{
size_t n;
printf("Enter the number of elements you want in your array\n");
if( scanf("%zu",&n) != 1){
fprintf(stderr,"Error in input");
}
if( n <= 0){
fprintf(stderr, "%s\n", "Error in input : Enter number >= 0 .");
}
int a[n];
printf("Enter the elements in your array\n");
for(size_t i = 0; i < n; i++)
{
if( scanf("%d",&a[i]) != 1){
fprintf(stderr,"%s\n","Error in input");
exit(1);
}
}
printMax(n,a);
return 0;
}
void printMax(size_t n,int A[])
{
int max=A[0];
for(size_t i = 1; i < n; i++)
if(A[i] > max)
max = A[i];
printf("%d",max);
}
In main() you declare n but it has no value, so likely defaults to 0. You then declare and define an array A and give it size n, which as I say is likely zero.
Within max() you then read in a value and assign it to n but your array A is size zero.
So change main() to
/* Get the number of items to store in the array */
int n;
printf("Enter the number of elements you want in your array\n");
scanf("%d",&n);
/* Create the array of the given size */
int A[n];
/* Now find the max value in that array */
max(n,A);
And remove the setting of n from max().
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Problem Link: https://www.codechef.com/problems/PERMUT2
Problem : Getting non ambiguous for all test cases. There is absolutely no problem in executing the program, no errors.
Can you please point out the mistake in my code/algorithm:
#include <stdio.h>
#include <stdlib.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++){
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n; j++){
if(nums[j] != index_func(j+1, nums, n)){
counter = 1;
break;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
return 0;
}
int index_func(int number, int *array, int x){
int z, index;
for(z=0; z<x; z++){
if(number == array[z]){
index = z;
return z;
}
}
}
The numbers in the array start with one, but the indices in C arrays start with 0. A quick fix to your program would be to add one to the returned index when you compare it to the current number:
if (nums[j] != index_func(j + 1, nums, n) + 1) ...
An alternative solution is to adjust the array data by subtracting one after you scan it, so that the array contains zero-based numbers.
A problem may arise with larger arrays, because every call to index_func scans the whole array from the beginning and will traverse half of it on average. The solution will be correct, but very slow.
But you don't have to determine the index to do the comparison. It is sufficient to check whether the number at the index of the current number is the current index. That leads to this function:
int is_ambiguous(const int *array, int n)
{
int i;
for (i = 0; i < n; i++) {
if (array[array[i] - 1] != i + 1) return 0;
}
return 1;
}
Some notes on your original code:
You should return an invalid index, probably −1, from index_funct when the nuber isn't in the array. I know, this shouldn't happen here, but next time you copy and paste the code and the missing return value might bite you.
You don't really need the variable index in index_funct. Separating pieces of code into small functions can make the program control easier. Compare the above function is_ambiguous with your inline solution with a counter variable and a break.
When you allocate, you must also free, which you don't.
try this solution:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++) {
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n-1; j++){
if((abs(nums[j+1] - nums[j]) != abs(n-1)) && (abs((nums[j+1] - nums[j]) != 1)))
{
counter = 0;
}
else
{
counter = 1;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
free(nums);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm working on a program but currently it asks for let you enter the values.
ex.
How many number you want to enter?
5
Type in the numbers.
1 2 3 4 5
But I want to pre-define the numbers like;
int i[5] = {1,2,3,4,5}
How to do that?
Here is the code.
#include <stdio.h>
#include <conio.h>
void main ()
{
int number[30];
int i,n,a,j;
printf ("Enter the value of n\n");
scanf ("%d",&n);
printf ("Enter the numbers\n");
for (i=0; i<n; ++i)
scanf ("%d", &number[i]);
printf ("Enter the position of the element to split the array \n");
scanf ("%d",&a);
for (i=0; i<a; ++i)
{
number[n] = number[0];
for (j=0; j<n; ++j)
{
number[j] = number[j+1];
}
}
printf("The resultant array is\n");
for (i=0; i<n; ++i)
{
printf ("%d\n",number[i]);
}
getch();
}
Do you have a maximum limit of the numbers to insert?
If yes, you can initialize your using array with predefined numbers, let's say:
int predefined[5] = {1, 2, 3, 4, 5};
and replace values when needed.
#include <stdio.h>
#include <conio.h>
int main ()
{
int number[5]={1,2,3,4,5};
int i,n=5,a,j;
printf ("Enter the position of the element to split the array \n");
scanf ("%d",&a);
for (i=0; i<a; ++i)
{
number[n] = number[0];
for (j=0; j<n; ++j)
{
number[j] = number[j+1];
}
}
printf("The resultant array is\n");
for (i=0; i<n; ++i)
{
printf ("%d\n",number[i]);
}
getch();
return 0;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to write some code that sorts a generated random number, between 0 and 1, and sorts it into a 'bin', which is an array. The window then prints out saying the number of random numbers in each bin. Hope this makes some sense, but I'm really struggling. I'm a complete beginner, and I'm completely stuck, and need some help on how to make the code work and where to go next. Here's what I've got so far.
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x,y;
int i;
int data[i];
unsigned int time_ui;
time_ui = (unsigned int)( time(NULL) );
srand(time_ui);
x = rand()/ (double)RAND_MAX;
data[i]=0;
for (i=0; i<10; i++)
{
(x*10)=y;
if ((int)y == i)
{
data[i]+=1;
}
printf("Bin %d contains %d random numbers\n", i, data[i]);
}
return 0;
}
I guess I understood what you were trying to do, here is what I could come with
#include <stdio.h>
#include <stdlib.h>
int main()
{
double x,y;
int i;
int data[10]; // you will generate 10 bins
unsigned int time_ui;
time_ui = (unsigned int)( time(NULL) );
srand(time_ui);
for (i =0; i<10; i++) // initialize each bin
{
data[i] = 0;
}
for (i=0; i<100; i++) // generate and examine 100 random you can do it for more
{
x = rand()/(double)RAND_MAX; // generate random number
y = 10 *x; // predict in which bin it will be
data[(int)y]++; // increase that bin by 1
}
for (i =0; i<10; i++) // once done, let's print it out
{
printf("Bin %d contains %d random numbers\n", i, data[i]);
}
return 0;
}
EDIT
Not really important, just for fun you can print out results like that :
for (i =0; i<10; i++) // once done, let's print it out
{
printf("\t");
for(j = 0; j<data[i]; j++)
{
printf("_ ");
}
printf("\nBin %d\t", i);
for(j = 0; j<data[i]; j++)
{
printf("_|");
}
printf("%d\n", data[i]);
}
Don't forget to define int j;