Read words from keyboard into a C 2D array - c

I would need some help after googling like 2 hours with no luck. (Maybe i'm just formulating my question incorrectly?)
I need to read 10 words from keyboard, and show a warning if any of the last 9 words are bigger than the first word.
I know how to use strlen to determine the last part of the request, but I have no idea how to do the first one.
My code looks like this:
const int ARRAY_SIZE = 10;
const int MAX_WORD_SIZE = 101;
int main() {
char arr[ARRAY_SIZE][MAX_WORD_SIZE];
for (int i = 0; i < ARRAY_SIZE; ++i) {
for (int j = 0; j < MAX_WORD_SIZE; ++j) {
printf("Add word %d:\n", i+1);
scanf("%s", arr[i][j]);
}
}
return 0;
}
But it does not work. cLion tell me "Format specifies type 'char *' but the argument has type 'int'".
I really don't understand why this doesn't work.

"%s" expects a memory address to where it is supposed to start saving the string.
You have a two dimensional array arr[ARRAY_SIZE][MAX_WORD_SIZE];. When you use [] once you get a 1-dimension array, which is what %s expects.
But since you are using [] twice, you are getting a single element of that array type, which is char.
change your scanf to: scanf("%s", arr[i]); to fix that.
Having noticed that, you can get rid of the inner for loop and variable j.
for (int i = 0; i < ARRAY_SIZE; ++i) {
printf("Add word %d:\n", i+1);
scanf("%s", arr[i]);
}

The solution to your problem would be:
const int ARRAY_SIZE = 10;
const int MAX_WORD_SIZE = 101;
int main() {
char arr[ARRAY_SIZE][MAX_WORD_SIZE];
for (int i = 0; i < ARRAY_SIZE; ++i) {
printf("Add word %d:\n", i+1);
scanf(" %s", arr[i]);
}
return 0;
}
It is a good practice to put a blank space in " %s". This way, chars such us \n are ommited, and many problems which stem from here are avoided.
If you wanted to do
for (int i = 0; i < ARRAY_SIZE; ++i) {
for (int j = 0; j < MAX_WORD_SIZE; ++j) {
printf("Add word %d:\n", i+1);
scanf("%s", arr[i][j]);
}
}
you would be reading character by character, so you would have to do instead:
for (int i = 0; i < ARRAY_SIZE; ++i) {
for (int j = 0; j < MAX_WORD_SIZE; ++j) {
printf("Add word %d:\n", i+1);
scanf("%c", arr[i][j]);
}
}
This way of solving the problem may give rise to another problem: not all words have the maximum size so this would be fixed by:
for (int i = 0; i < ARRAY_SIZE; ++i) {
for (int j = 0; j < MAX_WORD_SIZE; ++j) {
printf("Add word %d:\n", i+1);
scanf("%c", arr[i][j]);
if(arr[i][j]=='\n'){
break;
}
}
}
Anyway, the first one is the best solution.

Related

To printf a matrix

When i want to print a matrix which i input,i can use code:
#include <stdio.h>
int main(void)
{
int n, m; //row and column
printf("Enter row and column:\n");
scanf("%d %d", &n, &m);
int x[n][m];
printf("Enter your matrix:\n");
for (int i = 0; i < n; i++) //input my matrix
{
for (int j = 0; j < m; j++)
{
scanf("%d", &x[i][j]);
}
}
printf("print it:\n");
for (int i = 0; i < n; i++) //print it
{
for (int j = 0; j < m; j++)
{
printf("%d ", x[i][j]);
}
putchar('\n');
}
}
enter image description here(a possible case)
In code above, I have to assign values to the rows and columns of the matrix,which named "n" and "m".
int n, m;
scanf("%d %d", &n, &m);
But now I am asking a way to automatic tally .
Can I get this one directly?
enter image description here
You can simulate a two-dimensional array with a one-dimensional array, if that's what you mean:
#include <stdio.h>
#include <stdlib.h>
#define DIGITS 3
int main(void) { // It's good practice to fill function arguments with void if not planning on using them
/* scanf("%d %d", &n, &m); Using scanf with uninitialised variables will result in
* undefined behaviour if it is unable to convert the input. Using fgets
* is easier to debug, safer, and cleaner.
* I highly recommend this: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html
*/
char buf[DIGITS+1];
printf("%s", "Enter array rows:");
fgets(buf, DIGITS+1, stdin);
const int n = atoi(buf);
printf("%s", "Enter array columns:");
fgets(buf, DIGITS+1, stdin);
const int m = atoi(buf);
int x[n*m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("Enter array value at %d, %d: ", i, j);
fgets(buf, DIGITS+1, stdin);
x[i+j] = atoi(buf);
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
printf("%d ", x[i+j]);
}
printf("\n");
}
}
I'm not really sure as to why you would do this when C supports your two-dimensional array answer equally.
But now I am asking a way to automatic tally. Can I get this one directly?
Yes.
Form a linked-list of lines. Initially the list is empty.
Read the first line of input, the "1 2 3" into a string. Use fgets().
Parse the line to detect the number of values in it.
Append the line to the linked list of lines.
Continue doing so until 1) end-of-file, 2) a blank line or 3) number of integer is not the same as the first (error condition).
Now code has the m (number of values per line) and n, the number of lines.
Form int x[n][m];
Parse the lines for values and save in x.

C language copying arrays - error in code

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];

How to fill a two dimensional array with chars in C?

The point of the code is to create a 2D array of dimensions set by the user. After being prompted for the width and height, the user must put in char values into each position in the 2D array. When implemented with int: (int mat, int value), the algorithm works fine, however when trying to implement with char(char mat, char value), the output is not consistent with the integer version. The only changes are the type of array and value, as well as setting the scanf/printf("%d") to scanf/printf("%c). Any way you can implement this 2D array with chars?
#include <stdio.h>
int main() {
int width;
int height;
printf("Enter col and row values between 2 and 20.\n");
scanf("%d %d", &width, &height);
printf("The numbers you typed was %d %d\n", width, height);
char mat[height][width];
int i;
int j;
char value;
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
scanf("%c", &value);
mat[i][j] = value;
}
}
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("%c", mat[i][j]);
}
printf("\n");
}
return 0;
}
... the output is not consistent with the integer version
Well, of course not. It will be characters rather than integers. Otherwise, it will be the same.
Also, your input loop is really weird.
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
scanf("%c", &value);
mat[i][j] = value;
}
}
It seems like you're trying to prompt for each character, but then you only read a single character. I can't imagine how you expect anyone to use that. If I want to put in an "A", I type "A", but then nothing will happen until I press enter. But that will input two characters. So this code seems somewhat poorly designed.
As a quick fix, you could try something like:
for (i = 0; i < height; i++) {
for (j = 0; j < width; j++) {
printf("Enter your value.\n");
do scanf("%c", &value);
while (value == '\n' || value == '\r');
mat[i][j] = value;
}
}
Change this statement the following way
scanf(" %c", &value);
^^^^
Also change this statement something like
printf("Enter your character value: ");
that it would be more clear what the program expects to be entered.

C - Printing a 2D Char Array

How do I print the elements of a 2D Char Array in C?
Here is my current code:
int main()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d",&size);
char word[size][size];
//Enter the matrix
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%c",&word[k][j]);
}
}
//printf("\n");
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
//printf("\n ");
}
printf("\n");
}
When executed it returns the element in pairs (using a 4x4 array)
Example:
ab
cd
ef
gh
ij
kl
mn
op
Rather than my desired output:
abcd
efgh
ijkl
mnop
Why is this?
changing your scanf solves all the problems
scanf(" %c",&word[k][j]); // notice the space before '%c'
And also you need to change your printing loop to this
for (k = 0; k < size; ++k){
for(j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n");
}
Beware: %c and %1s do different things (apart from adding a terminating null for the latter):
c take every character including space, tab, cr and lf
%1s skip over all blanks (space, tab, cr, lf, etc.)
So at input time, you should use:
char c[2]; // provide room for a terminating null...
...
for(int k = 0; k < (size); ++k){
for (int j = 0; j < (size); ++j){
printf("Enter letter:");
scanf("%1s",c);
word[k][j] = c[0];
}
}
And at print time:
for (int k = 0; k < size; ++k){
for(int j = 0; j < size; ++j){
printf("%c",word[k][j]);
}
printf("\n "); // new line after each line
}
I removed the reading and it seems like printing is ok:
int main()
{
const unsigned int size = 4;
char word[size][size];
//Enter the matrix
for (int k = 0; k < (size); ++k) {
for (int j = 0; j < (size); ++j) {
word[k][j] = 'a' + j + (k * size);
}
}
for (int k = 0; k < size; ++k) {
for (int j = 0; j < size; ++j) {
printf("%c", word[k][j]);
}
printf("\n");
}
printf("\n");
getchar();
return 0;
}
And the output:
abcd
efgh
ijkl
mnop
I found two issues with your source.
One is the memory allocation - that is actually not ansi-c.
If you need dynamic memory you need to allocate it at runtime. Consider switching to c++ since there are standard facilities that help with that in a safer way.
The second issue was that there is a whitespace character in the buffer that is used as an input character. I think you want to clear that.
Here is the source with additional comments:
#include <stdio.h>
#include <stdlib.h>
void ansiC()
{
unsigned int size;
printf("Enter size:\n");
scanf("%d", &size);
//char word[size][size]; <- this is not ansi-c because size is unknown at compile time
char * word = (char*)malloc(sizeof(char)* size * size);
//Enter the matrix
for (int k = 0; k < (size); ++k)
{
for (int j = 0; j < (size); ++j)
{
printf("Enter letter:");
scanf("%c ", &word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
//after the character the user presses the enter key
//this puts a whitespace character on the buffer.
//by adding the space after %c you also clear that from the buffer
}
}
//printf("\n");
for (int k = 0; k < size; ++k)
{
for (int j = 0; j < size; ++j)
{
printf("%c", word[k * size + j]);
//since word is just a pointer i changed the way the position is calculated
}
//printf("\n ");
}
printf("\n");
free(word); //if you use malloc you need to remember to use free
}
int main()
{
ansiC();
return 0;
}
Please check this .
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
char arr[5][3]={"abc","aks","tny","dkn","kbf"};
for(int a=0;a<5;a++)
{
for(int b=0;b<3;b++)
{
cout<<" "<<arr[a][b];
}
cout<<endl;
}
getch();
}

Segmentation failure

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int score,i,j;
int *ptr, *ptr1;
int over;
printf("Enter the number of over");
scanf("%d",&over);
ptr=(int*)malloc(over*sizeof(int));
// do the iteration, outer for loop, read row by row...
for(i=0; i <= (over-1); i++)
{
printf("%d%d ", i, ptr[i]);
// inner for loop, for every row, read column by column and print the bar...
printf("Enter the number of run per over");
scanf("%d",&score);
ptr1=(int*)malloc(score*sizeof(int));
for(j = 1; j<= ptr1[i]; j++)
// print the 'bar', and repeat...
printf("|");
// go to new line for new row, and repeats...
printf("\n");
}
return 0;
}
You are using
ptr1=(int*)malloc(score*sizeof(int));
inside your for loop. That causes memory leak. You should free the memory.
You also have
printf("%d%d ", i, ptr[i]);
But ptr[i] has not been assigned any value, so it just gives garbage value. The same problem occurs in
for(j = 1; j<= ptr1[i]; j++)
So you need to assign some value to them before using them like this.
Casting the result of malloc doesn't make any sense, it is pointless and potentially bad practice.
printf("%d%d ", i, ptr[i]);. You print the value of an uninitialized memory cell. This is undefined behavior and might in theory cause the program to crash on some platforms. If you need the memory allocated to be initialized to zero, you should be using calloc() instead.
ptr1=(int*)malloc(score*sizeof(int)); for(j = 1; j<= ptr1[i]; j++) This code makes no sense whatsoever and will crash the program. You use ptr1 as if it was an initialied array of integers, while it is actually an uninitialized, single integer.
#include <stdio.h>
#include <stdlib.h>
int main(void){
int **scores;
int over, score;
int i, j;
printf("Enter the number of over : ");
scanf("%d", &over);
scores = (int**)malloc(over*sizeof(int*));
for(i = 0; i < over; i++){
printf("%d ", i + 1);
printf("Enter the number of run per over : ");
scanf("%d", &score);
scores[i] = (int*)malloc((score+1) * sizeof(int));// +1 for number of columns
scores[i][0] = score;
for(j = 1; j <= score; j++){
printf("%d Enter the score : ", j);
scanf("%d", &scores[i][j]);
}
}
for(i = 0; i < over; i++){
for(j = 1; j <= scores[i][0]; j++){
printf("|%d", scores[i][j]);
}
printf("|\n");
}
//deallocate
for(i = 0; i < over; i++)
free(scores[i]);
free(scores);
return 0;
}

Resources