adding two arrays in C - 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];

Related

how to print an array as a matrix in C

so I´m trying to print this 2d array as a matrix and it´s not working. any hints? no matter what i change i cant get to print an all 0 3x3 matrix
int main()
{
int i, j, m, n, primeira;
int matrix[10][20];
printf("Enter number of rows : ");
scanf("%d", &m);
printf("Enter number of columns : ");
scanf("%d", &n);
/* first input */
printf("1 ou 0");
scanf("%d", &primeira);
if (primeira = 0) {
matrix [0][0]=0;
matrix [0][1]=0;
matrix [1][0]=0;
matrix [1][1]=0;}
/* Display the matrix */
{
printf("%d\t", matrix[i][j]);
}
printf("\n");
return 0;
}
You would need to create a nested loop to display the matrix. If you want to display a 3x3 matrix you can run something like this.
int matrix[3][3] = { 0 };
/* Display the matrix */
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf ("%d\t", matrix[i][j]);
}
printf ("\n");
}
regarding:
printf("%d\t", matrix[i][j]);
The variables: i and j are not initialized!
Suggest:
for( int i =0; i<10; i++ )
{
for( int j=0; j<20; j++ )
{
printf( "%d ", matrix[i][j] );
}
puts( "" );
}
as that will print all the elements of the matrix and move to a new line after printing each row of the matrix.

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

Sorting a 2D-Array into Ascending order

I am trying to print out the contents of an Array into Ascending order, i am also using Functions (part of a task i have been assigned) - i am fairly new to C/C++ hence if anything is a mess.
The Sorting algorithm i have used just outputs all the numbers into a random order, any way to resolve or correct this? - Ideally this needs to output into the 2x2 grid as the Array Displays.
Ascending Order Output
include <stdio.h>/*Seperate Function for Input ArrayInt */
int main(){
int arrayHeight, array[100][2], xCoord, yCoord, i;
printf ("***** Bubble Sort ***** \n");
printf("\n How many items of data do you wish to enter? ");
scanf("%d",&arrayHeight);
for(i=0; i<arrayHeight; i++){
printf("Please enter in the X coordinate: ");
scanf("%d", &xCoord);
printf("Please enter in the Y coordinate: ");
scanf("%d", &yCoord);
array[i][0] = xCoord;
array[i][1] = yCoord;
}
DisplayArray(array, arrayHeight);
BubbleSort(array, arrayHeight);
}
int DisplayArray(int array[100][2],int arrayHeight, int swap) {
/*Displaying Array elements*/
int i, j;
printf("\n The 2-D Array contains : \n");
for(i=0; i<arrayHeight; i++)
{
printf("[%d][%d]\n\r", array[i][1], array[i][0]);
}
}
int BubbleSort(int array[100][2], int arrayHeight)
{
/*Sorts the Array elements into appropriate chosen sorting order - Ascending*/
int swap, i, j, k;
for(k =0; k< arrayHeight; k++) {
for (i = 0; i <arrayHeight; i++) {
for (j = i+1; j < 3; ++j) {
if (array[i][0] > array[i][-1]) {
array[i][1] = swap;
swap = array[i][0]; /*Array = Array Name*/
}
}
}
}
printf("\n Printing in Asending Order: ");
{
for (i=0; i<arrayHeight; i++)
{
for (j=0; j<arrayHeight; j++)
{
printf("\n [%d][%d] ", array[i][-1], array[i][1]);
}
}
}
}

Tried to create sorting program, but isn't working as expected

The following code is written for sorting array.
Here the program gives unexpected output results.
The error should be in line 10-19.
#include<stdio.h> //Sorting array program
int main()
{
int arr[20],i,j,n,temp;
printf("Enter number of elements : ");
scanf("%d",&n);
printf("\nEnter the elements of the array : ");
for (i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n;i++)
{
for(j=0;j<arr[i];j++)
if (arr[i+1]<arr[i])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
}
printf("\nThe sorted list is : \n");
for (i=0;i<n;i++)
printf("\n arr[%d] : %d",i,arr[i]);
return 0;
}
Output ::
Enter number of elements : 4
Enter the elements of the array : 5
3
2
1
The sorted list is :
arr[0] : 3
arr[1] : 2
arr[2] : 1
arr[3] : -16777216
Process returned 0 (0x0) execution time : 8.161 s
Press ENTER to continue.
Modify your program likes this and you'll find out what happens:
#include <stdio.h> //Sorting array program
#include <assert.h>
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{ // <<< line added
for (j = 0; j < arr[i]; j++)
{
assert(j < n); // <<< line added
assert(i + 1 < n); // <<< line added
if (arr[i + 1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
} // <<< line added
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
return 0;
}
Read about assert here.
But anyway the algorithm looks fishy to me anyway:
This line is particularly suspicious:
for (j = 0; j < arr[i]; j++)
Try this
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h> //Sorting array program
int main()
{
int arr[20], i, j, n, temp;
printf("Enter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements of the array : ");
for (i = 0; i<n; i++)
scanf("%d", &arr[i]);
for (i = 0; i<n; i++)
{
for (j = 0; j<n; j++)
if (arr[i]<arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
printf("\nThe sorted list is : \n");
for (i = 0; i<n; i++)
printf("\n arr[%d] : %d", i, arr[i]);
printf("\n");
return 0;
}

Transferring contents of an array to another in 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]);
}
}

Resources