Here I want to convert some student names into uppercase using a 2D array. This compiles without an error, but at the execution it gives an error called 'segmentation fault'. I tried this using a 1D array but it only prints the last name which I've entered. At the end I want to calculate total integer value for the entered names separately. I'm fairly new to C language, so there may be so many errors. Any ideas to fix my code?
#include<stdio.h>
#define SIZE 6
void input(char arr[][10], int size);
void upper(char arr[][10], int size);
void findIntValue(char arr[][10], int size);
int main(void)
{
char arr[SIZE][10] = {0.0};
input(arr, SIZE);
upper(arr, SIZE);
findIntValue(arr, SIZE);
return 0;
}
void input(char arr[][10], int size)
{
int i, j;
for(i = 0 ; i < size ; i++)
{
printf("\nEnter the name: ");
scanf(" %s", arr[i]);
}
}
void upper(char arr[][10], int size)
{
int i, j;
for(i = 0 ; i < size && arr[i] != '\0' ; i++)
{
for(j = 0 ; j < 10 && arr[i][j] != '\0' ; j++)
{
if(arr[i][j] >= 97 && arr[i][j] <= 122)
{
arr[i][j] -= 32;
}
}
}
for(i = 0; i < size && arr[i] != '\0' ; i++)
{
for(j = 0 ; j < 10 && arr[i][j] != '\0' ; j++)
{
printf("\n%s\n", arr);
}
}
}
void findIntValue(char arr[][10], int size)
{
int i, j;
int total = 0;
for(i = 0 ; i < size && arr[i] != '\0' ; i++)
{
for(j = 0 ; j < 10 && arr[i][j] != '\0' ; j++)
{
total += arr[i][j];
printf("\n%s\t%d\n", arr, total);
total = 0;
}
}
}
There are many problems in your code.
Problem-1: In the function input, you need to pass arr[i] as input to scanf
Problem-2:In the upper function, in the first loop, you are trying to read an uninitialized value of j which could lead to undefined behavior.
Problem-3:In the upper function, in the second loop, in the printf you are again using arr, whereas you should be using arr[i] and also, you don't need two loops for printing. One loop is enough.
Problem-4:The loop and print logic of findIntValue is wrong. You need to print the total and arr after the inner loop is completed. Also, printed value should be arr[i] and not arr.
Please find the corrected program below:
#include<stdio.h>
#define SIZE 6
void input(char arr[][10], int size);
void upper(char arr[][10], int size);
void findIntValue(char arr[][10], int size);
int main(void)
{
char arr[SIZE][10] = {0.0};
input(arr, SIZE);
upper(arr, SIZE);
findIntValue(arr, SIZE);
return 0;
}
void input(char arr[][10], int size)
{
int i, j;
for(i = 0 ; i < size ; i++)
{
printf("\nEnter the name: ");
scanf(" %s", arr[i]);
}
}
void upper(char arr[][10], int size)
{
int i, j;
for(i = 0 ; i < size && arr[i][0] != '\0' ; i++)
{
for(j = 0 ; j < 10 && arr[i][j] != '\0' ; j++)
{
if(arr[i][j] >= 97 && arr[i][j] <= 122)
{
arr[i][j] -= 32;
}
}
}
for(i = 0; i < size && arr[i][0] != '\0' ; i++)
{
printf("\n%s\n", arr[i]);
}
}
void findIntValue(char arr[][10], int size)
{
int i, j;
int total = 0;
for(i = 0 ; i < size && arr[i][0] != '\0' ; i++)
{
for(j = 0 ; j < 10 && arr[i][j] != '\0' ; j++)
{
total += arr[i][j];
}
printf("\n%s\t%d\n", arr[i], total);
total = 0;
}
}