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;
}
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;
}
The Question is to take dimension of array and elements of it from the user. Then calculate the sum of each column in array, it stops midway for some reason. I am new to C, is there a problem in usig malloc not in right way?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
int arr[m][n];
int **arr;
int i,j;
printf("Enter Number of Rows and Columns:\t");
scanf("%d %d", &m, &n);
arr=(int**)malloc(sizeof(int*)*m);
for(i=0;i<10;i++)
{
arr[i]=(int*)malloc(sizeof(int)*n);
}
printf("Type Array Elements:\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("This is Where It Stops");
//ADDITION OF COLUMNS
int Sum;
int *sum=(int*)malloc(j*sizeof(int));
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Sum=0;
Sum=Sum+arr[i][j];
}
sum[j]=Sum;
}
for(j<0;j<0;j++)
{
printf("Sum of Column %d is %d", j, sum[j]);
}
return 0;
}
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.
Here is the link to the codechef problem - Chef and Dolls
Why does my code always print the wrong answer? The output should be the number which doesn't have a match but my code always print the first element. Coudn't get the conditions right, what should be the condition?
Problem Statement
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Example
Input:
1
3
1
2
1
Output:
2
#include <stdio.h>
int main(){
int t, N, i, m, k, z, flag=0;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d", &arr[i]);
}
int j;
for(j=0;j<N;j++){
m=arr[j];
for(k=0;k<N;k++){
if(m==arr[k] && k!=j)
{
flag=0;
}
else
{
flag=1;
break;
}
printf("%d", m);
}
}
}
return 0;
}
You break out of your loop everytime k==j. You have to check for every array member if it is present somewhere else in the array.
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
for(j=0;j<N;j++)
{
m=arr[j];
flag=1;
for(k=0;k<N;k++)
{
if(m==arr[k] && k!=j)
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
}
}
}
return 0;
}
But be careful as this only looks if an array member has a duplicate in it. You probably want to check if there is an even number of elements in the array.
If you want to check which array members occur an odd number of times
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, num, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N], odds[N], n_odds;
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
n_odds=0;
for(j=0;j<N;j++)
{
m=arr[j];
num=0;
for(k=0;k<N;k++)
{
if(m==arr[k])
{
num++;
}
}
if(num%2) //occurs an odd number of times
{
flag=1;
for(i=0;i<n_odds;i++)
{
if(m==odds[i]) //has already been checked
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
odds[n_odds++]=m;
}
}
}
}
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.