How to define a start index in my c program? [closed] - c

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;
}

Related

Why does my array display does not add up? [closed]

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;
}

Dynamic Memory Allocation Of an Array in C [duplicate]

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);
}

Sorting random numbers into bins - C [closed]

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;

How can I call an Array into another function with a return value?

So, I'm trying to recall an array generated in one function into another function to work with it. Now, I've looked around and I can see that you can't call an entire array, but can call its pointer. I am not entirely sure how to do this and my attempts have led me to failure.
So I come to you, here is what my code looks like so far (or at least the two functions I am currently trying to work with):
int CreatePermutation(int n){
int arr[25];
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[25];
}
int main (){
int arr[25];
int n;
CreatePermutation(n);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
When I tried the CreatePermutation as a main function to see if it worked, it was generating and printing the permutations correctly, so all I really need is a way to get it into the main function.
You can pass the array as an argument:
int CreatePermutation(int *arr){
int n;
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return n;
}
int main (){
int arr[25];
int n;
n = CreatePermutation(arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
return 0;
}
This way, you can modify the array using the array pointer. You can also return the size of the permutation in your CreatePermutation function.
I think you want something like this:
int CreatePermutation(int n, int* arr)
{
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main (){
int arr[25];
int n = 25; /* Update thanks to PHIFounder */
CreatePermutation(n, arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
I will show simple example to make things clearer , you can get a clue from it and apply in your problem .
If you want then you can do it like this :
#include <stdio.h>
int CreatePermutation(int arr[] , int n)
{
for ( int i = 0; i < n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main ()
{
int arr[25];
int n ;
printf ("Please enter number of elements to be printed : ");
scanf ("%d", &n);//You should enter the number of elements before passing it to the function and in this way you can limit the number of elements you want to print .
CreatePermutation ( arr , n );
for (int i = 0; i < n; i++)
printf ("%d\n" , arr[i] );
}
NOTE-- My example shows a simple way to pass array as per your intention , you can further adjust it according to your needs :)

Copying of array leads to 0's? [closed]

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)

Resources