Why does scanf function asks for an extra input? - c

**#include <stdio.h>
#define SIZE 3
void scanA(int array[SIZE][SIZE]); // prototyping
int main()
{
int myarray[SIZE][SIZE];
int i,j;
printf("Please enter the array: \n");
scanArray(myarray);
for(i=0; i<SIZE; i++)
for(j=0; j<SIZE; j++)
{
printf("%c",myarray[i][j]);
}
printf("\n");
return 0;
}
void scanA(int array[SIZE][SIZE]) // function defintion
{
int i;
int j;
for(i=0; i<SIZE; i++) // looping to scan
for(j=0; j<SIZE; j++)
{
scanf("%c\n ",&array[i][j]);
}
}**
//The scanf in the scanA function asks for 10 chars although it is looped 9 times
I want to know the reason and a solution.

Try to put a blank space before the %c (only when you read a char), cause sometimes scanf reads the enter as a char. (I'm not totally sure about it, but with me this solution works)
Sorry for my english, it isn't my main language :(

Related

I need to create a number pattern program in C for my school assignment. Pattern- 1 141 14941

The pattern is
___1
__141
_14941
I have tried to some extent but looks like i have created a odd number pattern program
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=1; i<=N; i++)
{
// Prints the first part of pattern
for(j=1; j<=(i*2)-1; j+=2)
{
printf("%d", j);
}
// Prints the second part of pattern
for(j=(i-1)*2-1; j>=1; j-=2)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
I can see that the pattern has numbers that are off by odd numbers such as 3 and 5 respectively.
But i cant seem to grasp exactly how to do it.
Any help is much appreciated.
#include <stdio.h>
int main()
{
int i, j, N=3;
for(i=0; i<N; i++)
{
//For printing Spaces
for(int sp=0;sp<N-i;sp++){
printf(" ");
}
/*For printing the numbers
from left till the middle column*/
for(int j=1;j<=i+1;j++){
printf("%d",j*j);
}
/*For printing the numbers
from the strict right to the middle column
and till the end*/
for(int j=1;j<i+1;j++){
printf("%d",j*j);
}
printf("\n");
}
return 0;
}

Dynamic Memory Allocation Of an Array in C [duplicate]

This question already has answers here:
Why does scanf ask twice for input when there's a newline at the end of the format string?
(7 answers)
Closed 1 year ago.
I have been trying to write a simple code wherein the array is allocated dynamically. Every time I specify the side of the array as n(suppose 4) and proceed to type the given input, it takes exactly n+1(5 in this case) inputs from me but as the output, it prints n(4) elements.
Here's the main function I wrote:
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++) {
scanf("%d ", &arr[i]);
}
for(int i=0 ; i<n; i++) {
printf("%d ", arr[i]);
}
}
I've also tried doing the code by initializing i in the first loop as 1, and in that way it takes exactly n inputs but it gives a weird output, something like this:
7953616 1 2 3
in those two lines
scanf("%d ", &arr[i]);
printf("%d ", arr[i]);
you have %d instead of %d
also you need to use free() from stdlib library as after you finish using the pointer you need to free the memory in order to reuse it again otherwise this will happen
final code
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr, n;
scanf("%d", &n); //n is the size
arr = (int *)malloc(n*sizeof(int));
for(int i=0; i<n; i++)
scanf("%d", &arr[i]);
for(int i=0 ; i<n; i++)
printf("%d", arr[i]);
free(arr);
}

how many times a number, given by user from keyboard, is occured in the array

The program should calculate: How many times a number, given by user from keyboard, occurs in the array. I have seen similar examples but in this case it is not about the frequency of numbers of the given array, it's about same numbers between users numbers and the given array. So I couldn't make it.
How can we do this just using arrays, if-else condition and for loop?
#include <stdio.h>
int main()
{
int N=6, size;
int a [] = {1,2,3,4,5,6};
int occ [size];
int i,j;
int number;
int occured = 0;
printf ("enter the size of array\n");
scanf ("%d", &size);
printf("Enter elements in array:\n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}
for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}
printf ("given numbers occured in array %d times.", occured);
return 0;
}
This will solve you problem.
#include <stdio.h>
int main()
{
int N = 6, size;
int a [] = {1,2,3,4,5,6};
int i, j;
int occured = 0;
printf ("enter the size of array\n");
scanf ("%d", &size);
int occ [size];
printf("Enter elements in array:\n");
for (j=0; j<size; ++j){
scanf ("%d", &occ[j]);
}
for (j=0; j<size; ++j) {
for (i=0; i<N; ++i) {
if (occ[j] == a[i])
occured = occured + 1;
}
}
printf ("given numbers occured in array %d times.", occured);
return 0;
}
Your code has some mistakes which have been cleared by #Bwebb so I won't come into that.
Coming back to your problem, this can easily be solved by storing frequency using c++ map container. But since you are trying to implement it using only arrays there is an another way around.
You can make an another array with a fairly large size and initialise all it's elements to 0. Then you take the input from the user for the array and change into it's ASCII value if it's an alphabet when you put it in the index. For everytime the same input comes, you will increase the value of the corresponding index by 1, like this:a[1]++. And whenever you have to find the frequency of a character, you just have to put it in the index of a and print the output.

Function that takes file pointer and 2D array

I want to write a simple program that creates a 2D array and then prints it out.
I want to write a function that reads from an input file and creates the array.
Then, in the main function, I want to call the function to make the array and then print it out.
I know this is basic stuff but I can't find this answer anywhere in my textbook or online.
The input file is just a text file written exactly like the array' 12 row with 8 random numbers in each row.
#include <stdio.h>
void makeArray(FILE*ptr, int array[12][8]){
int i,j;
ptr = fopen("scores.txt", "r");
for (i=0; i<12; i++){
for (j=0; j<8; j++){
fscanf(ptr, "%d", array[i][j]);
}
}
}
int main(){
int i, j;
int scores[12][8];
FILE*input;
void makeArray(input, scores);
for (i=0; i<12; i++){
for (j=0; j<8; j++){
printf("%d\t", scores[i][j]);
}
printf("\n");
}
return 0;
}

Pointer taking input infinitely

I have following C program:
#include <stdio.h>
int main()
{
char p[12], i;
for(i=0; i<12; i++)
{
scanf("%d", p+i);
}
for(i=0; i<12; i++)
{
printf("%d", *(p+i));
}
return 0;
}
This code is taking input infinitely. Whats wrong with this?
You are messing up int and char (format specifier %d is for integers, not characters). Please refer to here for more info of format specifiers for different types.
If you want to read integers from user, you need to change your code to:
#include <stdio.h>
int main()
{
int p[12], i;
for(i=0; i<12; i++)
{
scanf("%d", &p[i]);
}
for(i=0; i<12; i++)
{
printf("%d", *(p+i)); // or better to use p[i] here
}
return 0;
}
The counter i is declared as a character in your code. This will lead to a logical error while executing the for loop.
So decare the variable i as follows,
int i;
Also change the format specifier to %c to handle the input properly.

Resources