C language copying arrays - error in code - c

I am trying to copy elements from two arrays into a third.
I can't understand why it doesn't work.
I made sure that the two arrays are filled properly, but for some reason, the actual copying doesn't work- when I print the elements of the arr3, I get some random numbers.
#include <stdio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n;
printf("Enter a number of elements to be stored in each array (up to 10): ");
scanf("%d", &n);
printf("Enter the %d elements to the first array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr1[i]);
}
printf("Enter the %d elements to the second array:\n", n);
for (i = 0; i < n; i++)
{
printf("Element %d: ", i + 1);
scanf("%d", &arr2[i]);
}
/*
// A test to make sure first 2 array are filled by the user- works
for(i = 0; i < n; i++)
printf("%d ", arr1[i]);
for(i = 0; i < n; i++)
printf("%d ", arr2[i]);
*/
// something wrong here, the elements are not coppied to the third array
for(i = 0; i < n; i++);
arr3[i] = arr1[i];
for(i = n; i < 2 * n; i++)
arr3[i] = arr2[i];
for(i = 0; i < 2 * n; i++)
printf("%d\n", arr3[i]);
return(0);
}

You're reading past the end of arr2, try this;
for (i = 0; i < n; i++)
arr3[i] = arr1[i];
for (i = 0; i < n; i++)
arr3[n+i] = arr2[i];

That's because your code is reading arr2[10],arr2[11] ..... arr2[19] (if n=10 ), all these values do not exist because arr2 only has 10 values. you can use this.
for (i=0; i<n; i++)
arr3[n+i]=arr2[i];
or
for (i=n; i<n*2; i++)
arr3[i]=arr2[i-n];

Related

Line pattern in a diamond in C

I'm a beginner in C, I'm facing a problem in order to implement a diamond, I'm following guided exercises from a book but I got stuck, I have to implement the pattern:
According to the exercise, they suggest me to implement the pattern using the operator %
I coded the structure but when I want to implement the pattern #.o.# I just destroy everything
#include<stdio.h>
int main(){
int n, space;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("#");
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("#");
}
printf("\n");
space++;
}
}
I really appreciate it if somebody could give me a hint or help with this, I got stuck here for the last 2 weeks.
Thank you.
This is what you need
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
}
The output for some cases:
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
./test
Number of sides: 5
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
./test
Number of sides: 2
#
#.#
For a diamond:
#include<stdio.h>
int main(){
int n, space,i,j;
char pattern[]={'#','.','o','.'};
int position;
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (i = 0; i < n; i++){
position=0;
for (j = 0;j < space; j++){
printf(" ");
}
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space--;
}
space = 0;
for (int i = n-1; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
position=0;
for (j = 0;j <= i; j++,position++)
printf("%c",pattern[position%sizeof(pattern)]);
position-=2;
for(;j<=2*i;j++,position--)
printf("%c",pattern[position%sizeof(pattern)]);
printf("\n");
space++;
}
}
And the output
./test
Number of sides: 10
#
#.#
#.o.#
#.o.o.#
#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.#.o.#.o.#
#.o.#.o.#.o.#.o.#
#.o.#.o.o.#.o.#
#.o.#.o.#.o.#
#.o.#.#.o.#
#.o.#.o.#
#.o.o.#
#.o.#
#.#
#
you have to just change printf("#") to printf("%c",pattern[j%4])
Here is a code:
int main(){
int n, space;
char pattern[4] = {'#','.','o','.'};
printf("Number of sides: ");
scanf("%d", &n);
space = n - 1;
for (int i = 0; i < n; i++){
for (int j = 0;j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j <= i*2; j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space--;
}
space = 0;
for (int i = n; i >= 0; i--){
for (int j = 0; j < space; j++){
printf(" ");
}
// patern #.o should be in this loop
for (int j = 0;j < (i*2)-1;j++){
printf("%c",pattern[j%4]);
}
printf("\n");
space++;
}
}
The reason why you "destroy everything" is probably because you tried to do it with printf(pattern[j%4]). printf as first parameter wants string (address in memory when string is stored) when you pass pattern[j%4], it thinks that for example '#' (in ASCII 64) is adress in memory, and want to read from it, but it is not valid memory adress so operating system kill your program and it "get's destroyed"

C language 2d array fill diagonal with numbers from 1 to n

I have a 2d array filled with 0's and i'm trying to fill the main diagonal with numbers from 1 to n, this is the main code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int m, n, i, j;
printf("Number of rows and columns:");
scanf("%d", &n);
int a[n][n];
for (i = 0; i < n; i++)
for(j = 0; j < n; j++)
a[i][j] = rand() % 1;
printf("The matrix is:\n");
for (i = 0; i < n; i++)
{
printf(" \n ");
for(j = 0; j < n; j++)
{
printf(" %d\t ", a[i][j]);
}
}
}
What I've tried to do is to fill the diagonal manually, but that's not what I want to do. I want to make it fill itself automatically. I need to do it without using any functions.
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = ((i == j) * (i + 1));
}
}
The simplest way is to add this part after you fill the matrix with zeros.
for (i = 0; i < n; i++)
arr[i][i] = i + 1;

Printing Two C Columns One Sorted

I'm working on taking 10 numbers as user input and having the input printed in two columns with one sorted column.
#include <stdio.h>
int main(void)
{
int NUM_ELEMENTS = 10;
int list[NUM_ELEMENTS];
int sortedList[NUM_ELEMENTS];
int i, j, temp;
printf("Enter 10 numbers:\n");
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];}
}
}
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) {
temp = list[i];
sortedList[j] = temp;
}
}
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]);
}
return 0;
}
The first column prints correctly but the second column doesn't print the correct numbers. I'm fairly new at coding in C and can't seem to get this to work right. I've tried several variations of the provided code. The columns are also supposed to be side by side and labeled as well.
There are lot of bugs in the above code.
-firstly, looping constraints are wrong in scanning and assigning part
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d \n", &list[i]); {
for(j = 0; j < NUM_ELEMENTS; j++){
sortedList[j] = list[i];} /* for i=0 why list[0] assigned to all sortedList[j] ?
}
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; i++) {
scanf("%d", &list[i]);
sortedList[i] = list[i];
}
-Secondly,sorting logic is not correct at all.
for(i = 0; i < NUM_ELEMENTS; i++) {
if(list[i] > sortedList[j]) { /* what is sortedList[j] ? */
temp = list[i];
sortedList[j] = temp;
}
}
Replace above one with simple sorting technique logic for now but once become strong with basics check performance view also, which sorting method is better to use.
for(i = 0; i < NUM_ELEMENTS-1; i++) {
for(j = 0;j < NUM_ELEMENTS-1-i; j++) {
if(sortedList[j] > sortedList[j+1]) {
temp = sortedList[j];
sortedList[j] = sortedList[j+1];/* you miss this */
sortedList[j+1] = temp;
}
}
}
-Finally, there is bug in print part also.
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[j]); /* what is j here ? you can use the same variable i for both. */
}
Replace above one with
for(i = 0; i < NUM_ELEMENTS; ++i){
printf("%d %d\n", list[i], sortedList[i]);
}

#EMERGENCY!!! Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted

I know this seems like an old question, but none answered questions I searched work.
I have kept receiving "Run-Time Check Failure #2 - Stack around the variable 'b' was corrupted." when I was trying to do a [4][2]*[2][3] matrix multiplication.
Does anyone spot the problem?
#include <stdio.h>
int main() {
int a[4][2] = {0};
int b[2][3] = {0};
int c[3][3] = {0};
int i, j;
printf("Please enter first matrix value\n");
for (i = 0; i < 4; i++) {
for (j = 0; j < 2; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
printf("Please enter second matrix value\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
printf("%d row, %d column:", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
}
printf("\n the result is :\n");//
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
printf(" %4d ", c[i][j]);
}
printf("]\n");
}
return 0;
}
I haven't checked your code thoroughly, but you define c as 3x3, and here
for (i = 0; i < 4; i++) {
printf("[");
for (j = 0; j < 3; j++) {
c[i][j] = (a[i][0] * b[0][j]) + (a[i][1] * b[1][j]);
...you access c[3], which is c's fourth element, and does not exist. This is bound to write somewhere else.
So check your indexes (as #ptb observed, c's should actually be four rows deep).

Copying each row of matrix to a temporary array

I want to copy a row (one each team iterating) of a 500x8 matrix to a temp array with the name actual_row. This is what I've tried.
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
// do other stuff
}
}
printf("\n");
}
This is not printing the line, it's printing 0's and 1's sometime, so there's something I'm doing wrong.
Thanks in advance.
Don't print actual_row before it's filled completely:
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
}
printf("The row is: ");
for(int q = 0; q < 8; q++) {
printf(" %d ",actual_row[q]);
...
}
Your logic is slightly off. You need to copy the row to actual_row, then print the contents. Furthermore, why not just print the contents while you are copying the matrix row to actual_row:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
So your code snippet should be this:
int matrix[500][8]; // this has been already filled by int's
int actual_row[8];
for(int i = 0; i < 500; i++) {
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}
// <--at this point, actual_row fully contains your row
printf("\n");
}
Your logic is slightly off (no need for the third nested loop). You need to copy the row to actual_row (which you did), and print the contents within the same loop:
printf("The row is: ");
for(int j = 0; j < 8; j++) {
actual_row[j] = matrix[i][j];
printf(" %d ",actual_row[j]);
// do other stuff
}

Resources