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 am learning C and one of my functions (not shown here) depends on two arrays being the same. I have one array and am trying to generate a copy but what happens is that both arrays turn out to be full of 0's after the copy. I have no idea how this happens.. Can anybody help explain why this happens and offer a solution on how to do it right?
#include <stdio.h>
int main(){
int array[5]={3,2,7,4,8};
int other_array[5]={0,0,0,0,0};
for (int i=0; i<5; i++) array[i]=other_array[i]; //Copies array into other_array
printf("array is:\n");
for (int j=0; j<5; j++) printf("%d", array[j]);
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", other_array[i]);
printf("\n");
return 0;
}
/*
When run this yields
**********************
array is:
00000
other_array is:
00000
*/
//Copies array into other_array
for (int i=0; i<5; i++) array[i]=other_array[i];
Try:
other_array[i] = array[i];
The assignment operator assigns the value of the right operand into the object in the left operand. And not the opposite.
Also, as said in other answers:
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", array[i]);
Your are printing array and not other_array.
You're printing the wrong array, you print array twice
printf("array is:\n");
for (int j=0; j<5; j++) printf("%d", array[j]);
printf("\nother_array is:\n");
for (int i=0; i<5; i++) printf("%d", other_array[i]);//<-- here
printf("\n");
Of course you could just write:
int main(){
int array[] = { 3, 2, 7, 4, 8};
int other_array[] = { 0, 0, 0, 0, 0};
memcpy(other_array, array, sizeof other_array);
/* then print */
}
It might also be a good idea to #include <assert.h> and then:
assert(sizeof array == sizeof other_array)
Related
This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 1 year ago.
I have been trying to write a simple code wherein the array is allocated dynamically. Every time I specify the side of the array as n(suppose 4) and proceed to type the given input, it takes exactly n+1(5 in this case) inputs from me but as the output, it prints n(4) elements.
Here's the main function I wrote:
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++) {
scanf("%d ", &arr[i]);
}
for(int i=0 ; i<n; i++) {
printf("%d ", arr[i]);
}
}
I've also tried doing the code by initializing i in the first loop as 1, and in that way it takes exactly n inputs but it gives a weird output, something like this:
7953616 1 2 3
in those two lines
scanf("%d ", &arr[i]);
printf("%d ", arr[i]);
you have %d instead of %d
also you need to use free() from stdlib library as after you finish using the pointer you need to free the memory in order to reuse it again otherwise this will happen
final code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++)
scanf("%d", &arr[i]);
for(int i=0 ; i<n; i++)
printf("%d", arr[i]);
free(arr);
}
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 6 years ago.
Improve this question
#include <stdio.h>
int main()
{
int a[5];
int i;
for (i=0; i<5; i++)
a = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a);
}
Arrays used as operand of operatiors except for sizeof and unary & are automatically converted to a pointer that points at the first element of the array.
The converted pointer is not a lvalue, so it cannot be used as the left operand of assignment operator.
This code works.
#include <stdio.h>
int main(void)
{
int a[5];
int i;
for (i=0; i<5; i++)
a[i] = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a[i]);
return 0;
}
You declare a to be an array of integers - and instead of accessing an element of the array a[i] you access a directly, which only holds the memory adress of the first element of the array. So you are basically modifying memory adresses directly, which is almost never a good idea.
yu have error in a =i, should a[i]=i:
int main()
{
int a[5];
int i;
for (i=0; i<5; i++)
a[i] = i;
for (i=0; i<5; i++)
printf("a[%d] = %d\n", i, a[i]);
}
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;
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
How do I scan the values of an array? Like:
Input: 3 4 5 6 7
What I want:- ar[5] = {3, 4, 5, 6, 7};
It sounds easy, but I'm stuck here. Can anyone help?
You can read it as you were reading five integers one after the other:
for (i = 0; i < 5; i++)
scanf("%d", &array[i]);
So you can input 3 4 5 6 7 normally.
All above are valid answers, you could also use dynamically allocated array if you don't know how many elements there is. There's a lot of different versions such as increasing the array size by 1 with each new element or inputing the size at the start...
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ar, i, j, h;
scanf("%d", &i); // Input the size of an array
ar = (int*)malloc(sizeof(int)*i); // allocate the memory for your array
for(j = 0; j < i; j++){
scanf("%d", &h);
*(ar+j) = h;
}
for(j = 0; j < i; j++) printf("%d\n", ar[j]);
free(ar);
return 0;
}
And here's an example where you increase the size by 1 with each new element using realloc();. For this example lets say you input numbers until you enter -1.
#include<stdio.h>
#include<stdlib.h>
int main(){
int *ar, i, s = 1;
ar = (int*)malloc(sizeof(int));
do{
scanf("%d", &i);
if(i == -1) break;
ar[s-1] = i;
realloc(ar, ++s);
}while(1);
for(i = 0; i < s - 1; i++) printf("%d\n", ar[i]);
free(ar);
return 0;
}
Very important thing with dynamically allocated arrays is that you need to free the memory using free(); before you exit the program.
You need to declare the array size before (recommended with #define), that mean you need to know the size of the input before.
#define LEN 5
void main()
{
int arr[LEN];
for (i =0; i < LEN; i++)
scanf("%d", &arr[i]);
}
If you want to create an array dynamically you must use pointers (with malloc and realloc).
void main()
{
int* arr = NULL;
size_t size = 0;
int val;
while (scanf("%d", &val) != EOF)
{
int* newArr = realloc(arr, size + 1);
if (!newArr) { /* handle memory exception here */ }
arr = newArr;
size++;
}
free(arr);
}