Transferring contents of an array to another in C - c

I'm coding a program which accepts a number as a divisor and the other numbers from the user. My problem is with segregating the array where the ten entered numbers into two different arrays, one array is for numbers divisible by a divisor entered by the user and one is for non-divisible ones. I think I've got most of it down but whenever I try to display the contents of the array it would show a 0 at the end of the line. Also when none of the entered numbers are divisible it would dispaly "16 0 1" even if those numbers are not entered by the user.
Here's my code:
int main(){
int num, arr[size], div[size], nondiv[size], d=0, nd=0;
int divsize = 0;
int nondivsize = 0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i; i<10; i++){
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
printf("Number of divisible numbers: %d ", divsize);
printf("\nDivisible numbers: ");
for(int i=0; i<divsize; i++){
printf("%d ", div[i]);
}
}

I did some changes to your code and i think it works fine.....
in your code:
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
i don't think this is necessary as you've already caluclated number of divisble and non divisble numbers and stored them in d and nd respectively (and) the numbers in the arrays div[] and nondiv[] in this loop:
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
so while printing the number of divisible numbers and the divisible numbers array you can us d as parameter instead of divsize like this:
printf("Number of divisible numbers: %d ", d);//changed to d
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++) //even here
{
printf("%d ", div[i]);
}
so to sum it all up, remove the last but one loop and change the
parameters of last loop from divsize to d in the last loop
*** and by the way I hope you declared size globally
so the code would be:
#include<stdio.h>
#define size 10 //i defined size globally here
int main()
{
int num, arr[size], div[size],nondiv[size],d=0, nd=0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i=0; i<10; i++){
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else
{
nondiv[nd] = arr[i];
nd++;
}
}
printf("Number of divisible numbers: %d ", d);
printf("\nDivisible numbers: ");
for(int i=0; i<d; i++){
printf("%d ", div[i]);
}
}
-thank you

You declared a loop variable but didn't initialize it so its values is garbage value. Where you wrote
for(int i; i< size ;i++)
you should use i=0.
Other mistake is you can not declare size globally mean declare size as global variable and use as Loop variants means
for(int i=0;i< size;i++)
I also add commenting in your program where I change.
This loop isn't required, it makes the program too complex:
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
Because You already have counts of div array with the name of d.
Here is your updated Program
#include<stdio.h>
#include<conio.h>
#define size 10 // Every where you use hard coded 10 change it to size
int main(){
int num , arr[size], div[size], nondiv[size], d=0, nd=0;
int divsize = 0;
int nondivsize = 0;
int arrsize = 0;
do{
printf("Enter a number within 1 and 5: ");
scanf("%d", &num);
if(num<1 || num>5)
printf("\nThe number you have entered is not within the given range.\n");
} while(num<1 || num>5);
printf("\nEnter ten numbers: \n");
for(int i=0; i<10; i++){
printf("Number %d: ", i+1);
scanf("%d", &arr[i]);
}
printf("\nEntered numbers: ");
for(int i=0; i<10; i++){
printf("%d ", arr[i]);
}
//calculates the arrays size of arr and displays it
for(int i=0; i<10; i++){
// In loop variable you did'nt initialize i that's why it shows garbage value which is greater than 10000
if(arr[i]!= 0)
arrsize++;
}
printf("\narrsize: %d\n", arrsize);
//Stores divisible and non-divisible inputs in to different arrays
for(int i=0; i<10; i++){
if(arr[i]%num == 0){
div[d] = arr[i];
d++;
}
else{
nondiv[nd] = arr[i];
nd++;
}
}
//calculates the number of elements in array div and displays it
for(int i=0; i<10; i++){
if(div[i] != 0){
divsize++;
}
}
printf("Number of divisible numbers: %d ", d/*Here I just print d Because it is the count of divisible*/);
printf("\nDivisible numbers: ");
for(int i=0; i<d/*Here also used d */ ; i++){
printf("%d ", div[i]);
}
}

Related

printing the largest value in array

I was having a trouble regarding to printing out the largest number in array in c language and i don't know how to do it while using loop. Please help me thanks
here is my code
#include <stdio.h>
void main() {
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for (int i = 0; i < size; i++) {
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for (int i = size - 1; i >= 0; i--) {
printf("%d ", nums[i]);
}
for (int a = 0; a < size; a++) {
for (int i = 0; i < size; i++) {
if (nums[a] > nums[i]) {
Lnum = nums[a];
}
}
}
printf("\nLargest element = %d", Lnum);
}
The algorithm for determining the largest element in an array goes as follows.
Use the value of the first array element as a start value for the biggest number.
Go over the rest of the array and check for each element if it is a bigger number.
When a bigger number is found use the bigger number for further comparisons.
#include<stdio.h>
void main(){
int Lnum, size;
printf("Enter the size: ");
scanf("%d", &size);
int nums[size];
for(int i=0; i<size; i++){
scanf("%d", &nums[i]);
}
printf("\nReversed = ");
for(int i=size-1; i>=0; i--){
printf("%d ", nums[i]);
}
Lnum = nums[0]; // Assume first element to be the largest element
for(int a=1; a<size; a++){ // start loop with second element
if(Lnum < nums[a]) { // check if current element is larger
Lnum = nums[a]; // found larger element, it is now the largest element of all inspected element
}
}
printf("\nLargest element = %d", Lnum);
}
Output:
Enter the size: 3
1 2 3
Reversed = 3 2 1
Largest element = 3
...Program finished with exit code 0

Error in calculating resultant matrix in C programming language

I am trying to write a program where I have two matrices and I multiply the two matrices and store it in a resultant matrix named "carr." For some weird reason, the matrix multiplication is not getting executed properly. Tried to find the issue for quite a while but couldn't find the error. Can anyone help? TIA for your time!
Here is the ss of the issue: https://snipboard.io/s9ifP4.jpg
#include <stdio.h>
int main()
{
int row1, column1, row2, column2,i,j,k, sum=0;
//START OF THE 1ST ARRAY//
printf("How many rows do you want for the first matrix? Ans: ");
scanf("%d", &row1);
printf("How many columns do you want for the first matrix? Ans: ");
scanf("%d", &column1);
int arr[row1][column1];
printf("Enter the elements of the first array:\n");
for(i = 0; i <row1; i++){
for(j=0; j < column1; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the first array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column1; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
//END OF THE FIRST ARRAY//
printf("----------------------------------------\n");
//START OF THE 2ND ARRAY//
printf("\n**How many rows do you want for the second matrix?\n\nAlert: For matrix multiplication, the COLUMN of the 1st matrix MUST equal to the ROW of the 2nd matrix.\nAns: ");
scanf("%d", &row2);
printf("How many columns do you want for the second matrix? Ans: ");
scanf("%d", &column2);
int barr[row2][column2];
printf("Enter the elements of the second array:\n");
for(i = 0; i <row2; i++){
for(j=0; j < column2; j++){
scanf("%d", &arr[i][j]);
}
}
printf("\n----------------------------------------\n");
printf("The elements of the second array are:\n");
for(i = 0; i <row2; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", arr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
//END OF THE 2ND ARRAY//
//Everything above this part is okay. The problem starts from the Matrix multiplication part//
//MATRIX MULTIPLICATION//
//The resultant matrix where the values of the multiplied matrix is being held has row = ROW1 and column = COLUMN2.//
int carr[row1][column2];
if(column1 == row2)
{
for(i = 0; i < row1; i++){
for(j=0; j < column2; j++){
for(k=0; k < row2; k++){
sum = sum + arr[i][k] * barr[k][j];
}
carr[i][j] = sum;
sum=0;
}
}
}
else
{
printf("Matrix multiplication is not possible");
}
printf("\n----------------------------------------\n");
printf("The elements of the resultant array are:\n");
for(i = 0; i <row1; i++){
printf("[ ");
for(j=0; j < column2; j++){
printf("%d, ", carr[i][j]);
}
printf("]\n");
}
printf("----------------------------------------\n");
return 0;
}
Changing arr to barr fixes the issue. Thanks to #M Oehm for pointing out the error.

Inserting elements in the array from input [Core Dump (Segmentation fault)]

I wrote an algorithm that sorts elements in an array. Now I'm trying to solve it by taking elements from input but the compiler gives me this error -> "Core Dump (Segmentation fault)"
int array[100];
int num;
printf("How many elements do you want to insert? --> ");
scanf("%d", num);
for(int i=0; i<num; i++){
printf("Insert the element --> ");
scanf("%d", &array[i]);
}
for(int i=0; i<num; i++){
for (int j=i+1; j<num; j++){
if (array[i] > array[j]){
int tmp = array[i];
array[i] = array[j];
array[j] = tmp;
}
}
}
for(int i=0; i<num; i++){
printf("%d\n", array[i]);
}
Right here:
scanf("%d", num);
You need to pass a pointer to an int. This should be:
scanf("%d", &num);

Shift to the right elements of the vector

The user enter a sequence of N numbers saved in a vector. The program first does a shift to the left of vector elements and assigns 0 to the last element. Then it does a shift to the right of the new vector elements and assigns 0 to the first element.
Problem: I can't do correctly the shift to the right 'cause the output is wrong, but I don't understand what mistake I made. See //SHIFT TO THE RIGHT// and vet[0]=0.
#include <stdio.h>
int main(void)
{
const int maxn=300; //max vector dimension
int N; //vector dimension
int vet[maxn];
int i; //index
do
{
printf("how many numbers will be entered?");
scanf("%d", &N);
if (N>maxn || N<=0)
{
printf("ERROR.The number must be between 0 and %d\n", maxn);
}
printf("\n");
}
while (N>maxn || N<=0);
printf("Enter a sequence of %d numbers\n", N);
printf("\n");
for (i=0; i<N; i++)
{
printf("element number %d: ", i+1);
scanf("%d", &vet[i]);
}
printf("\n");
printf("the sequence entered is:\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
//SHIFT TO THE LEFT//
//1 2 3 4 -> 2 3 4 0//
for(i=0; i<N-1; i++)
{
vet[i]=vet[i+1];
}
vet[N-1]=0;
printf("The resulting sequence from the shift to the left is:\n");
printf("\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
//SHIFT TO THE RIGHT//
// 2 3 4 0 -> 0 2 3 4//
for (i=N-1; i>0; i--);
{
vet[i]=vet[i-1];
}
vet[0]=0;
printf("The resulting sequence from the shift to the right is: \n");
printf("\n");
for (i=0; i<N; i++)
{
printf("%d ", vet[i]);
}
printf("\n");
return(0);
}

adding two arrays in C

The problem is basically printing two arrays on an external array as right part and left part but although I can print one part, I can not print the other part. The scenario is that for example 5 elements on the left and 7 at the right. Then it reverses the right side and sticks it to the beginning of the array.
Here is the piece of code that I typed.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int mainarray[] = {} , n , i , rank;
printf("\nHow many element[10..2000]: ");
scanf("%d", &n);
printf("\nEnter the array elements : ");
for(i=0; i<n; ++i)
{
scanf("%d" , &mainarray[i]);
}
printf("\nThe array is : ");
for(i=0 ; i<n ; i++)
{
printf("%d ",mainarray[i]);
}
printf("\n\nEnter the rank of the element : ");
scanf("%d",&rank);
printf("\n\nElement on rank %d is: %d",rank, mainarray[rank-1]);
int leftarray[] = {} , rightarray[] = {};
for(i=0; i<=rank ; i++)
{
leftarray[i] = mainarray[i];
}
printf("\n\nThe left array is : ");
for(i=0 ; i<=rank ; i++)
{
printf("%d ", leftarray[i]);
}
for(i=0 ; i<n-rank-1 ;i++)
{
rightarray[i]=mainarray[i+rank+1];
}
printf("\n\nThe right array is : ");
for(i=0 ; i<n-rank-1 ;i++)
{
printf("%d ", rightarray[i]);
}
int j=n-rank-2,temp;
i=0;
while(i<j)
{
temp=rightarray[i];
rightarray[i]=rightarray[j];
rightarray[j]=temp;
i++;
j--;
}
printf("\n\nNew right array is : ");
for(i=0 ; i<n-rank-1 ; i++)
{
printf("%d ", rightarray[i]);
}
i=0;
while(i<n-rank-1)
{
mainarray[i]=rightarray[i];
i++;
}
j=0;
while(j<rank+1 && i<n-1)
{
mainarray[i]=leftarray[j];
j++;
i++;
}
printf("\n\nThe result is : ");
for(i=0 ; i<n ; i++)
{
printf("%d ",mainarray[i]);
}
printf("\n\n\n");
system("pause");
return 0;
}
You're declaring empty arrays when you write:
int mainarray[] = {};
As a result, you're storing outside the array bounds, which results in undefined behavior.
You need to declare the arrays after you get the size from the user.
scanf("%d", &n);
int mainarray[n]; // no need to initialize here, you're going to fill it in with the input loop
Then do similar things for leftarray and rightarray:
scanf("%d",&rank);
int leftarray[rank];
int rightarray[n-rank];

Resources