Copy all array elements into another array in C - 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;
}

Related

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

Printing the given value using DMA

I'm coding to get the value of given number in Dynamic method using realloc. In this case the O/P of new size elements is not printing the correct value. Its printing some unknown garbage values after one or two elements.
This program to resize the integer value in the run time.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *ptr,n,newsize;
printf("Enter size Elements\n");
scanf("%d", &n);
ptr = (int*) malloc (n * sizeof(int));
printf("Enter the Elements\n");
for(int i=0;i<n;i++)
{
scanf("%d", ptr+i);
}
printf("Elements are\n");
for(int i=0;i<n;i++)
{
printf("%u\n", *(ptr+i));
}
printf("Enter the New Size of the Elements\n");
scanf("%d", &newsize);
ptr = realloc(ptr,newsize);
printf("Enter the new Elements\n");
for(int i=0;i<newsize;i++)
{
scanf("%d", ptr+newsize);
}
printf("New Elements are\n");
for(int i=0;i<newsize;i++)
{
printf("%u\n", *(ptr+i));
}
return 0;
}
Expecting:
1
2
3
4
Actual Results:
1
2
4566745
4534665
Its working now after making below correction said by Johnny Mopp.
printf("New Elements are\n");
for(int i=0;i<newsize;i++)
{
printf("%d\n", *(ptr+i));
}

C Program, User input into a single array

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.

Why does my C dynamic array give me access violation?

Here is the code I have.
int *arr; // Indented this line
int sizeOfArr = 0;
printf("Enter size of arr.\n");
scanf("%d", &sizeOfArr);
arr = malloc(sizeOfArr * sizeof(int));
for (int i = 0; i < sizeOfArr; i++) {
scanf("%d", arr[i]);
}
For example, if the size of the dynamic array is 5, and I proceed to enter the input "1 2 3 4 5", the entire program crashes and gives me access violation.
In order to store array elements you need to use scanf("%d", &arr[i]); instead of
scanf("%d", arr[i]);
& in C is used to refer to address of a variable. So,by using &arr[i] you are telling your program to store the input variable at ith index of array arr[].
So the correct code will be
int *arr;
int sizeOfArr = 0;
printf("Enter size of arr.\n");
scanf("%d", &sizeOfArr);
arr = malloc(sizeOfArr * sizeof(int));
for (int i = 0; i < sizeOfArr; i++) {
scanf("%d", &arr[i]); //notice the diff here
}

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