i want to copy element of first array to the second array with pointer
but after compiling i get message that is A problem caused the program to stop working correctly.windows will close the program and notify you if a solution is avilable
#include <stdio.h>
#include <stdlib.h>
int main()
{
int arr[30],arr1[30];
int n,i;
int *p,*q;
p=arr;
q=arr1;
printf("Enter the no. of elements of array:-\n");
scanf("%d",&n);
printf("Enter the array element of 1st array:-\n");
for(i=0;i<n;i++)
{
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
printf("elements of array 1=");
for(i=0;i<n;i++)
{
printf("%d , ",*(p+i));
}
printf("\nelements of array 2=");
for(i=0;i<n;i++)
{
printf("%d , ",*(q+i));
}
for(i=0;i<n;i++)
{
printf("%d , ",arr1[i]);
}
return 0;
}
Change
for(i=0;i<n;i++)
{
q=(int*)*(p+i);
q++;
}
To
for(i=0;i<n;i++)
{
q[i]=p[i];
}
As of now you are treating int as int * and assigning it to pointer leading to undefined behaviour.
Also *(p+i) is same as p[i] and (p+i) is same as &p[i]. But latter ones are more readable.
Related
function for get array from the user
#include <stdio.h>
void getArray()
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
}
function for display array
void displayArray(){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
getArray();
displayArray();
return 0;
}
The problem is how to pass the array that we get from the user to the display array function and both functions can be called in the main and also the array want to declare in the main function
An example that does not handle input errors.
In order for your functions to have knowledge of the array, you must send them its address as well as its size.
#include <stdio.h>
int getArray(int a[], int size_max)
{
int n;
printf("Enter size of array: ");
while(1)
{
scanf("%d",&n);
if(n>size_max) printf("The size must be less than %d: ", size_max);
else break;
}
printf("Enter %d elements in the array : ", n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
return n;
}
void displayArray(int a[], int n)
{
printf("\nElements in array are: ");
for(int i=0; i<n; i++) printf("%d ", a[i]);
}
int main()
{
int a[1000];
int n = getArray(a, 1000);
displayArray(a, n);
return 0;
}
You can pass that shared variable as argument. Also return and use the returned value if reference not having valid data. Or else you need to pass this argument as reference instead of value like this.
#include <stdio.h>
int[] getArray(int a[])
{
printf("Enter size of array: ");
scanf("%d",&n);
printf("Enter %d elements in the array : ", n);
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
return a;
}
function for display array
void displayArray(int a[]){
printf("\nElements in array are: ");
for(i=0;i<n;i++)
{
printf("%d ", a[i]);
}
}
Both functions are called in the main
int main(){
int a[1000],i,n;
a = getArray(a);
displayArray();
return 0;
}
For some Reason the Program Crashes at the Loop used to merge the two arrays in main.
though maybe this question is a foolish in one(idk it didn't allow me to post as my program is mostly code so now i am blabbering).
(Well it still doesn't so here's a Picture of a Cat
CAT )
//To make two arrays of user Defined Size and merge them in a third array
#include<stdio.h>
#include<stdlib.h>
void array(int**,int);
void ini(int**,int);
void display(int **,int);
int main()
{
int *a1,*a2,*a3,s1,s2,i,j;
printf("Enter Size of Array 1\n");
scanf("%d",&s1);
printf("Enter Size of Array 2\n");
scanf("%d",&s2);
ini(&a1,s1);
ini(&a2,s2);
ini(&a3,s1+s2);
printf("Enter Elements of Array 1\n");
array(&a1,s1);
printf("Array 1:\n");
display(&a1,s1);
printf("Enter Elements of Array 2\n");
array(&a2,s2);
printf("Array 2:\n");
display(&a2,s2);
for(i=0;i<s1;i++)
{
a3[i]=a1[i];
}
for(i=0,j=s1;i<s2&&j<s1+s2;i++,j++)
{
a3[j]=a2[i];
}
printf("Merged Array:\n");
display(&a3,s1+s2);
return 0;
}
void ini(int **a,int s)
{
*a=(int*)calloc(s,sizeof(int));
}
void array(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d",&a[i]);
}
}
void display(int **a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n",a[i]);
else
printf("%d\t",a[i]);
}
}
a1, a2, and a3 is a pointer of an integer type variable, which stores the starting address of your buffer.
a1[index] is the value in the address of a1 with 'index' as the offset.
Since you were passing &a to your array() and display() function, you were passing the address of your variable into the function instead of passing the address of your buffer.
You could change your array() and display() to:
void array(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
printf("Enter Element at position %d\n",i+1);
scanf("%d", &(a[i]));
}
}
void display(int *a,int s)
{
int i;
for(i=0;i<s;i++)
{
if(i==s-1)
printf("%d\n", a[i]);
else
printf("%d\t", a[i]);
}
}
and call it like this:
array(a1,s1);
display(a1,s1);
in a program to accept an array and display it on the console using functions getArray() and displayArray() how this program works as it Accepts values of array in function getArray() and uses it in the function displayArray() without returning any values from the first function?
I tried this program and failed to get result, then found this one in youTube comment section and I tried it and got results! I want to know how this program works ?
Q:Write a program to accept an array and display it on the console using function?
a.Program should contain 3 functions including main() function,
main() - {
Declare an array.
Call function getArray().
Call function displayArray() }.
getArray() -
Get values to the array.
displayArray() -
Display the array values
#include <stdio.h>
#include <stdlib.h>
void getArray(int);
void displayArray(int);
int main(void) {
int limit;
printf("Enter The Size of the Array :");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
return EXIT_SUCCESS;
}
void getArray(int limit){
int i,a[100];
printf("Enter The Values of Array :\n");
for(i=0;i<limit;i++){
scanf("%d",&a[i]);
}
}
void displayArray(int limit){
int i,b[100];
printf("Your Array is :\n");
for(i=0;i<limit;i++){
printf(" %d\t",b[i]);
}
printf("\n");
}
Array a in getArray is a local variable that gets destroyed when it goes out of scope. Array b in displayArray is also a local variable (local to displayArray) and has no relationship with a in getArray. You need to pass the same array to both functions.
One way could be to allocate the needed memory for the array in main and pass that, along with the number of elements in the array, to the two functions.
Example:
#include <stdio.h>
#include <stdlib.h>
// a is now a pointer to the first element in the array:
void getArray(int *a, int limit) {
printf("Enter The Values of Array :\n");
for(int i = 0; i < limit; i++) {
scanf("%d", &a[i]);
}
}
// b is now a pointer to the first element in the array:
void displayArray(int *b, int limit) {
printf("Your Array is :\n");
for(int i = 0; i < limit; i++) {
printf(" %d\t", b[i]);
}
putchar('\n');
}
int main(void) {
int limit;
printf("Enter The Size of the Array :");
if(scanf("%d", &limit) != 1 || limit < 1) return EXIT_FAILURE;
// allocate memory for `limit` number of `int`s:
int *arr = malloc(limit * sizeof *arr);
if(arr == NULL) return EXIT_FAILURE;
getArray(arr, limit); // pass arr + limit
displayArray(arr, limit); // pass arr + limit
free(arr); // and free the memory when done
return EXIT_SUCCESS;
}
#include<stdio.h>
void getArray(int);
void displayArray(int);
int array[100];
void main()
{
int limit;
printf("enter the array size you want\n");
scanf("%d",&limit);
getArray(limit);
displayArray(limit);
}
void getArray(int limit)
{
printf("enter the array element you want\n");
for(int i=0;i<limit;i++)
{
scanf("%d",&array[i]);
}
}
void displayArray(int limit)
{
for(int i=0;i<n;i++)
{
printf("%d",array[i]);
printf("\t");
}
}
I've been messing with functions recently and i wanted to transform the following code:
#include<stdio.h>
int main()
{
int i,n,tab[100];
printf("Insert The Number Of Elements :\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
return 0;
}
... into a code where i can use functions to:
Ask for the number of elements that the array is going to hold
Show the output of the array with the elements the user entered
Here's what i got to so far but it seems like it keeps asking me for the number of elements the array will hold even though it has been already entered.
Thanks in advance to those who will help <3
#include<stdio.h>
int Number_Of(int a)
{
printf("Insert The Number Of Elements :\n");
scanf("%d",&a);
return a;
}
int Array(int tab[])
{
int i,n;
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
}
int main()
{
int i,n,tab[100];
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
Array(tab);
return 0;
}
You are calling "Number_Of" function two times that's why you prompted to enter again.
First time you called in main function.
Second time you called in Array function.
You can do like this.
#include<stdio.h>
int Number_Of(int a)
{
printf("Insert The Number Of Elements :\n");
scanf("%d",&a);
return a;
}
void Array(int tab[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Index[%d]=%d\n",i,tab[i]);
}
}
int main()
{
int i,n,tab[100];
n=Number_Of(n);
for(i=0;i<n;i++)
{
printf("Give The Element N~%d:",i+1);
scanf("%d",&tab[i]);
}
printf("The Elements Are:\n");
Array(tab, n);
return 0;
}
This code does not give me answer when i enter value 1 in array.
for eg i have taken no. of elements as 5
then i enter them as 2,3,1,6,4
then output gives 2 as smallest number and position number is not always correct
what's the error?
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
small=a[0];
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
"small" variable is overridden with a[0] in each iteration. Just move it outside the loop:
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
You are not that far away - use are initializing small=a[0]; every time whenever loop iterates so just initialize small=a[0]; before loop
Here your corrected code
#include<stdio.h>
int main()
{
int n,i,a[10],sum=0;
int small=0,pos=0;
printf("enter no of elements in array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d]=",i);
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if( a[i] < small)
{
small=a[i];
pos=i;
}
}
printf("smallest no:%d \n",small);
printf("position:%d",pos);
}
small = a[0] should be before the loop.
You should write small = a[0] before the beginning of a for loop.