What is the problem with this lvalue error in ascending array? - c

#include <stdio.h>
#include <string.h>
int main(void)
{
char names[10][20] = { "kim", "lee", "sin", "jo", "kim2", "chae", "jin", "bak", "so", "choi" };
int i, j;
char tmp[20];
printf("\nfirst\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
for (i = 0; i < 9; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(names[j], names[j + 1]) > 0)
{
//tmp = names[j];//
//names[j] = names[j + 1];//
//names[j + 1] = tmp;//
//break;//
//This part.//
}
printf("\nSorted Result\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
return 0;
}
I have to make this code without strcpy. but this code has l-value error. Any hints or suggestions to this problem?

Instead of declaring tmp and names as array, use pointer, also because of break in for loop, it is not sorting all variables correctly.
Try with below program:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *names[10] = { "kim", "lee", "sin", "jo", "kim2", "chae", "jin", "bak", "so", "choi" };
int i, j;
char *tmp;
printf("\nfirst\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
for (i = 0; i < 10; i++)
for (j = 0; j < 9 - i; j++)
if (strcmp(names[j], names[j + 1]) > 0)
{
tmp = names[j];
names[j] = names[j + 1];
names[j + 1] = tmp;
}
printf("\nSorted Result\n");
for (j = 0; j < 10; j++)
printf("%5s", names[j]);
printf("\n");
return 0;
}

Related

Cannot input number in multi-dimensional array pointer to pointer

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
int N;
scanf("%d", &N);
int **arr;
arr = (int**)malloc(sizeof(int) * N);
for(int i = 0; i < N; i++){
arr[i] = (int*)malloc(sizeof(int) * N);
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
if(i == 0 || j == 0)
arr[i][j] = 1;
else
arr[i][j] = arr[i - 1][j] + arr[i][j - 1];
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
for(int i = 0; i < N; i++)
free(arr[i]);
free(arr);
return 0;
}
This code makes a 2-dimensional array with a pointer; I want assign value arr[i][j] = 1. However, an error EXC_BAD_ACCESS occurs in XCode on a Mac. I don't know how to solve the problem. What is my mistake? The assigning for loop is where the trouble occurs.

why does 10 becomes : in c program

char **array;
char *x_ptr = &array[0][0];
int rowcount = 0;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
if (j == 0) {
rowcount += 1;
*(x_ptr +( i*column + j)) = rowcount+'0';
}
else {
*(x_ptr +( i*column + j)) = 0;
}
}
}
When running this loop for the for the 10th time, why does it store the int value for 10 as symbol:
Current result
8,9,:,;,<,=,>
The ASCII for '0' is 48. If you add 10 to it, you'll get 58, which is the ASCII for ':'.
You should use char array[10][10]; not char **array;
':' == '9' + 1
The following code could work:
#include <stdio.h>
int main()
{
int row = 10, column = 10;
char array[10][10];
int rowcount = 0;
for (int i = 0; i < row; ++i)
for (int j = 0; j < column; ++j)
if (j == 0)
array[i][j] = ++rowcount + '0';
else
array[i][j] = 0;
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j)
printf("%c\t", array[i][j]);
printf("\n");
}
return 0;
}

Getting a SIGSEGV error in my C program

This is my code for the problem.
I used a dynamic programming approach, and my answer is coming out to be correct. But I am getting a Runtime Error of SIGSEGV, may be because of array index and I am unable to figure out how and where?
If you can figure out what and where is the problem, please let me know.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int dynp(int i, int j);
int v[] = {-1, 0, 1, 0};
int h[] = {0, 1, 0, -1};
int ROW, COL;
#define INF 1000000
int minOfTwo(int one, int two){
if (one<two) {
return one;
}else{
return two;
}
}
int matrix[190][190];
int dp[190][190];
int main(int argc, const char * argv[]) {
int t;
scanf("%d", &t);
int i,j;
char s[190];
while (t--) {
scanf("%d %d", &ROW, &COL);
for (i = 0; i<ROW; i++) {
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
dp[i][j] = INF;
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
if (dp[i][j] == INF) {
if (matrix[i][j] == 1) {
dp[i][j] = 0;
}
else{
dp[i][j] = dynp(i, j);
}
}
}
}
for (i = 0; i<ROW; i++) {
for (j = 0; j<COL; j++) {
printf("%d ", dp[i][j]);
}
printf("\n");
}
}
return 0;
}
int dynp(int i, int j)
{
if (dp[i][j] != INF) {
return dp[i][j];
}
else{
if (matrix[i][j] == 1) {
dp[i][j] = 0;
return dp[i][j];
}
else{
int k;
for (k = 0; k<4; k++) {
int newi = i + v[k], newj = j + h[k];
if (newi < ROW && newj < COL && newi>=0 && newj>=0) {
dp[i][j] = minOfTwo(dp[i][j], 1 + dynp(newi, newj));
}
}
return dp[i][j];
}
}
}
At the first look, in your code,
scanf("%s", s);
for (j = 0; j<COL; j++) {
matrix[i][j] = s[j] - 48;
looks problematic. With the length of s lesser than the value of COL and s being an automatic local variable not initialized explicitly, you'll be accessing allocated but uninitialized memory location.
You should change the looping condition to something like
scanf("%189s", s); //to avoid overflow
int len = strlen(s);
for (j = 0; (j<COL) && (j < len); j++, len--) {
matrix[i][j] = s[j] - 48;

2D Array rotation in C by 90

I wrote this code to rotate a square matrix by 90 degrees. but it started to show me runtime error.
I have absolutely no clue why i get that notice.Can someone please fix the code for me.
It shows me segmentation fault.i have no clue what that means.
#include <stdio.h>
int main()
{
int N, i = 0, j = 0;
scanf("%d", &N);
int A[N][N], B[N][N], temp;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
scanf("%d", (A[i][j]));
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
B[i][j] = A[j][i];
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
temp = B[i][j];
B[i][j] = B[N - i - 1][j];
B[N - i - 1][j] = temp;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%d", B[i][j]);
}
}
return 0;
}
replace this scanf("%d",(A[i][j])); with this scanf("%d",&A[i][j]);

How to turn 2d array out of 1d one?

To access any element I use *(Ptr + i).
Is there any way to put 2D array into the allocated memory in order to access any element using array[i][j]?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *Ptr;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i <= M * N; i++)
*(Ptr + i) = 1 + rand()%10;
return 0;
}
Sample
#include <stdio.h>
#include <stdlib.h>
#define N 4
#define M 3
int main()
{
int *Ptr;
int (*p)[M];
int i,j;
Ptr = malloc(M*N*sizeof(int));
for (i = 0; i < M * N; i++){
*(Ptr + i) = 1 + rand()%10;
// printf("%d ", Ptr[i]);
}
// printf("\n");
p=(int (*)[M])Ptr;//p[N][M]
for(i = 0; i < N ;++i){
for(j = 0; j < M;++j)
printf("%d ", p[i][j]);
printf("\n");
}
return 0;
}
Try this:
int** theArray;
theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
}
Sample:
int M = 5;
int N = 5;
int** theArray = (int**) malloc(M*sizeof(int*));
for (int i = 0; i < M; i++)
{
theArray[i] = (int*) malloc(N*sizeof(int));
for(int j = 0 ; j < N; j++)
{
theArray[i][j] = i+j;
printf("%d ", theArray[i][j]);
}
printf("\n");
}
for (int k = 0; k < M; k++)
{
free(theArray[k]);
}
free(theArray);

Resources