C Program, User input into a single array - c

I'm trying to use three integers that are inputted by a user in a single line into an array using CodeBlocks. But i don't really know how to go about this. Can anyone help me?
Thanks!
main()
{
int arr[3];
int onenum;
int twonum;
int threenum;
printf("Enter an Input: ");
scanf("%d %d %d",&onenum,&twonum,&threenum);
printf("Your input is: %d %d %d \n",onenum,twonum,threenum);
int arr [onenum, twonum, threenum];
return 0;
}

Use this
int i;
int arr[3];
printf("Enter numbers");
for(i=0;i<3;i++){
scanf("%d",&arr[i]);
}
This will store the 3 numbers entered by user in array arr.

Like this:
void main()
{
int arr[3];
int i;
printf("Enter an Input: ");
for(i = 0; i < 3; i++)
{
scanf("%d", &arr[i]);
}
printf("Your input is: ");
for(i = 0; i < 3; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

Let's use a loop from which runs from i=0 to i=2 because you have to add 3 numbers in array.
void main()
{
int arry[3],i=0;
printf("enter numbers");
for(i=0;i<3;i++)
{
scanf("%d",&arry[i]);
}
for(i=0;i<3;i++)
{
printf("%d \n",arry[i]);
}
}
Array is a collection of data.
For beginning you can think a array with different index as a different variable of same data type means arry[0] and arry[1] in this example as different variables of same data type integer.It will help you for beginning with array but keep in mind that arry is the only one variable the index inside capital bracket tells the variable where to look.

Related

How to write two functions for getting an array and display that array in c

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

Finding the frequency of an element in an array using a user-defined function in C

I am writing a program where we need to find the frequency of the number chosen in an array. The numbers were inputted by the user and he/she will choose a number to count its instance. I did it with this program.
#include<stdio.h>
int main()
{
int n, frq=0, chn;
int num[10];
int i, j;
printf("Enter quantity of numbers to be inputted: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number[%d]: ", i+1);
scanf("%d", &num[i]);
}
printf("(");
for(j=0; j<n; j++)
{
printf("%d, ", num[j]);
}
printf(")\n\n");
printf("Enter number to be counted: ");
scanf("%d", &chn);
for(i=0; i<n; i++)
{
if(num[i]==chn)
{
frq++;
}}
printf("Instance count: %d", frq);
return 0;
}
It works perfectly fine. Then our teacher tasked us to do the same. But this time, we will use user-defined functions for the part where the program will count the instances of the chosen number. This is what I did.
#include<stdio.h>
int freq(int n);
int main()
{
int n, frq=0, chn;
int num[10];
int i, j;
printf("Enter quantity of numbers to be inputted: ");
scanf("%d", &n);
for(i=0; i<n; i++)
{
printf("Enter number[%d]: ", i+1);
scanf("%d", &num[i]);
}
printf("(");
for(j=0; j<n; j++)
{
printf("%d, ", num[j]);
}
printf(")\n\n");
printf("Enter number to be counted: ");
scanf("%d", &chn);
frq = freq(n);
printf("Instance count: %d", frq);
return 0;
}
int freq(int n)
{
int chn, num[10], i, frq=0;
for(i=0; i<n; i++)
{
if(num[i]==chn)
{
frq++;
}}
return(frq);
}
But when I run it, it doesn't bring out the supposed output. For example;
input quantity: >>>6
Enter number[1]: >>>1
Enter number[2]: >>>2
Enter number[3]: >>>3
Enter number[4]: >>>3
Enter number[5]: >>>3
Enter number[6]: >>>2
(1, 2, 3, 3, 3, 2, )
Enter number to be counted: >>>3
Instance count: 1
Process Finished.
>>>
It was supposed to print 3 but instead, it prints 1. I don't know where I did wrong and I need your help guys. I really appreciate every answers and comments you put here, I run through every single one of them and it helped me. Thank you again!
ps. we were not allowed to use pointers or structures or idk in this program yet. Just in case some of you will suggest using them lol.
To write the function finding the frequency you need to pass:
The array
The size of the array
The number.
You simply declare the array inside the function thinking that it will magically have the content you have entered in another function. No, it contains undetermined values and invokes an UB
size_t freq(int *array, size_t size, int val)
{
size_t freq = 0;
while(size--) freq += *array++ == val;
return freq;
}
int main(void)
{
int num[] = {1, 2, 3, 3, 3, 2, };
printf("The %d is present in the array %zu times\n", 3, freq(num, sizeof(num)/sizeof(num[0]), 3));
}
or instead ( as #4386427 spotted) "oneliner"
while(size--)
{
if(*array == val) freq += 1;
array++;
}

Copy all array elements into another array in C

I done with my logic which is actually used to copy a Array elements into another Array but in the final output(Point 1) of printing statement is not working well as I expecting.
#include <stdio.h>
int main()
{
int arr[50],n,key,loc;
printf("Enter size of the Elements:\n");
scanf("%d", &n);
printf("Enter %d Elements\n", n);
for(int i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
//int a = arr[i];
printf("Enter the Element to insert:\n");
scanf("%d", &key);
printf("Enter the Location to insert:\n");
scanf("%d", &loc);
for(int i=n-1;i>=loc;i--)
{
arr[i+1] = arr[i];
}
arr[loc] = key;
printf("Result of Array:\n");
for(int i=0;i<=n;i++) //Point 1
{
printf("%d\n", arr[i]);
}
return 0;
}
Its expecting to print the copied value to print but its not showing the last element of the array.
eg:
a[] = 1,2,3
b[] = 8,9
Expecting o/p:
1,2,3,8,9
Actual o/p:
1,2,3,8
When you insert your element into your array, the size of your array increases; you should put a n++ before printing.
Are you sure that is the right code that you are trying to solve? I don't see anything wrong with that logic except the potential for buffer overflow from the input. I think your logic is correct. But instead of declaring a size for the array, you can just let the value from user input do that. I posted the code below, just a small fix to make your code to become dynamic because there isn't anything wrong with your code logic.
#include <stdio.h>
int main()
{
int n,key,loc;
printf("Enter size of the Elements:\n");
scanf("%d", &n);
int arr[n + 1];
printf("Enter %d Elements\n", n);
for(int i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
//int a = arr[i];
printf("Enter the Element to insert:\n");
scanf("%d", &key);
printf("Enter the Location to insert:\n");
scanf("%d", &loc);
for(int i=n-1;i>=loc;i--)
{
arr[i+1] = arr[i];
}
arr[loc] = key;
printf("Result of Array:\n");
for(int i=0;i<=n;i++) //Point 1
{
printf("%d\n", arr[i]);
}
return 0;
}

How to compare two arrays in C programming language?

I want to compare two different arrays which are both int. One array is static and contains numbers from 1 to 10 and second arrays asks user to enter ten different numbers and the program checks which elements from both arrays are equal.
#include <stdio.h>
int main(void) {
int array1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int array2[10];
int i;
for (i = 0; i < 11; i++) {
printf("Enter numbers: ");
scanf("%d", &array2);
}
for (i = 0; i < 11; i++) {
if (array1[i] != array2[i]) {
printf("Not equal \n");
} else {
printf("They are equal. \n");
}
}
}
The program always say not equal even if I enter a number that is equal to a number stored in first array.
scanf("%d", &array2);
You are never updating the index of array2 when getting a value from input.
Try
scanf("%d", &array2[i]);
As for comparing, you can also use memcmp to compare memory:
memcmp(array1, array2, sizeof(array1));
Arrays use zero based indices for a start. You are incorrectly populating array2 so you probably want to change your first loop to the following
for (i=0;i<10;i++)
{
printf("Enter numbers: ");
scanf("%d", &array2[i]);
}
as your current code is simply passing the address of array2 as argument to scanf.
Then change the second loop conditional to
for (i=0;i<10;i++)
in your comparison loop so that you do not access items beyond your array boundaries.
Currently your second loop is accessing items at indices 0 to 10 - but there are only 10 items present in array1 so you have undefined behaviour with your current code.
#include <stdio.h>
int main(void) {
int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[10];
int i;
for (i=0;i<10;i++) { //fixed the range here
printf("Enter numbers: ");
scanf("%d", &array2[i]); //fixed the indexing
}
for (i=0;i<10;i++) { //fixed the range here
if (array1[i] != array2[i]) {
printf("Not equal \n");
}
else {
printf("They are equal. \n");
}
}
}
I am a beginner and I have this idea about comparing two arrays. Hope It might help someone like me.
/***compare two array: all elements are same or not(if not sorted)***/
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int array1[n], array2[n];
int i, j;
for(i=0; i<n; i++)
{
scanf("%d", &array1[i]);
}
for(i=0; i<n; i++)
{
scanf("%d", &array2[i]);
}
int flg=0;
for(i = 0; i < n; i++)
{
for(j=0; j<n; j++)
{
if(array1[i] == array2[j])
{
flg += 1;
break;
}
}
}
if(flg == n)
{
printf("All The Elements of array1 is present in array2... :)");
}
else
{
printf("All THe Elements of array1 is not present in array2 :(");
}
return 0;
}
I am trying to respond to the answer, even though I am a beginner to C program.
According to your program written above, you are inputting and holding values to int array2[10] which has 11 elements.
Remember that the first element of this array is indexed by zero. Ex: array2[0], until it reaches the last element which is array2[10], you have counted 11.
Now array1 has the pre-defined values write which will be compared to your input values. Enter and store your values into array2[].
#include <stdio.h>
int main(void) {
int array1[] = {1,2,3,4,5,6,7,8,9,10};
int array2[10];
int i;
for (i=0;i<10;i++) { //fixed the range here
printf("Enter numbers: ");
scanf("%d", &array2[i]); //fixed the indexing
if (array1[i] != array2[i]) {
printf("Not equal \n");
}
else {
printf("They are equal. \n");
}
}
}

How to get each element of a array from user input then pass it to a function in another file in C

How can I get each element of an array from user input then pass it to a function in another file. I'm having trouble and need help please. This my code.
main.c
#include <stdio.h>
#include "lab8.h"
int x[100];
int y[100];
int main(void) {
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the first array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Element: %i\n", x[i]);
printf("Enter the second array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product: %i\n", product);
return(0);
}
lab8.c
#include <stdio.h>
#include "lab8.h"
int inner_product(int a[], int b[], int count) {
int i;
int result = 0;
for( i=1; i<count; i++) {
result = result + (a[i] * b[i]);
}
return result;
}
It only seems to multiply the last element entered for both arrays heres the output.
Enter the length of both arrays
2
Enter the first array's elements
1
2
Element: 0
Enter the second array's elements
3
3
Inner product: 6
The problem is that you are iterating from 1 and you should iterate from 0, in the inner_product() function
for( i=1; i<count; i++) {
/* ^ this should be 0 */
also, don't use global variables specially because you got the rest of it right, you are passing the arrays as arguments to the inner_product() function.

Resources