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);
}
Related
I tried to malloc a 2d array, but it gdb gives me Segmentation fault in line 8. It seems like something is wrong with if (A[i+x][j+y]!=A[i][j].
By the way: This program should print out the dimensions of the maximum square-sized sub-matrix of an input matrix, such that all its digits are equal matrix, such that all its digits are equal.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int same_surrounding(int **A, int i, int j, int size){
for (int x=0; x<size; x++){
for (int y=0; y<size; y++){
if (A[i+x][j+y]!=A[i][j])
return 0;
}
}
return 1;
}
int main(){
int n, i, j, sub, max_sub;
int **A;
scanf("%d", &n);
A=malloc(sizeof*A*n);
for (i=0; i<n; i++){
A[i]=malloc(sizeof**A*n);
}
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++){
sub=1;
while (sub<n&&same_surrounding(A, i, j, sub)){
if (sub>max_sub)
max_sub=sub;
sub++;
}
}
}
printf("%d", max_sub);
for (i=0; i<n; i++){
free(A[i]);
}
free(A);
return 0;
}
There are mainly 2 things wrong with this code:
Your program is checking for submatrices beyond the dimension of input matrix n. for example, if n=5,i=3,j=0 and sub=3 you can easily see that your code is checking for numbers in A[i+x][j+y] i.e. upto A[5][2] which is out of bound of A. You need to check this before sending it to same_surrounding(). For that all you need to do is slightlt change the while loop:
while (sub<=n && i+sub<n && j+sub<n && same_surrounding(A, i, j, sub)){...}
Always initialize your variables. Here you didn't initialize max_sub variable. When you don't initialize your variables, it contains random garbage value. For example if the garbage value is 478231 and you are looking for largest square matrix of 5x5 matrix, you'll never reach the condition if (sub>max_sub) and as a result max_sub will never update. That's why always initialize your variable with a safe value, for example, max_sub=0.
Also, as a warning, you should use sizeof() instead of explicitly writing the number of bytes necessary for memory allocation. Because they might change depending on computer architecture.
A=(int**) malloc(sizeof(int*)*n);
for (i=0; i<n; i++){
A[i]=(int*)malloc(sizeof(int)*n);
}
Here's the working version of your code. All I did was some { } cleanup. If you have just one statement under for, while, if etc., you don't really need the braces.
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.
How can I get each element of an array from user input then pass it to a function in another file. I'm having trouble and need help please. This my code.
main.c
#include <stdio.h>
#include "lab8.h"
int x[100];
int y[100];
int main(void) {
int count, i, product;
printf("Enter the length of both arrays\n");
scanf("%d", &count);
printf("Enter the first array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &x[i]);
}
printf("Element: %i\n", x[i]);
printf("Enter the second array's elements\n");
for(i=0; i<count; i++){
scanf("%i", &y[i]);
}
product = inner_product(x, y, count);
printf("Inner product: %i\n", product);
return(0);
}
lab8.c
#include <stdio.h>
#include "lab8.h"
int inner_product(int a[], int b[], int count) {
int i;
int result = 0;
for( i=1; i<count; i++) {
result = result + (a[i] * b[i]);
}
return result;
}
It only seems to multiply the last element entered for both arrays heres the output.
Enter the length of both arrays
2
Enter the first array's elements
1
2
Element: 0
Enter the second array's elements
3
3
Inner product: 6
The problem is that you are iterating from 1 and you should iterate from 0, in the inner_product() function
for( i=1; i<count; i++) {
/* ^ this should be 0 */
also, don't use global variables specially because you got the rest of it right, you are passing the arrays as arguments to the inner_product() function.
**#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 :(
So, I'm trying to recall an array generated in one function into another function to work with it. Now, I've looked around and I can see that you can't call an entire array, but can call its pointer. I am not entirely sure how to do this and my attempts have led me to failure.
So I come to you, here is what my code looks like so far (or at least the two functions I am currently trying to work with):
int CreatePermutation(int n){
int arr[25];
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[25];
}
int main (){
int arr[25];
int n;
CreatePermutation(n);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
When I tried the CreatePermutation as a main function to see if it worked, it was generating and printing the permutations correctly, so all I really need is a way to get it into the main function.
You can pass the array as an argument:
int CreatePermutation(int *arr){
int n;
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return n;
}
int main (){
int arr[25];
int n;
n = CreatePermutation(arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
return 0;
}
This way, you can modify the array using the array pointer. You can also return the size of the permutation in your CreatePermutation function.
I think you want something like this:
int CreatePermutation(int n, int* arr)
{
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
while(n<=0){
printf ("Please input a possitive number for a permutation size.\n");
printf ("Please enter desired permutation size: ");
scanf ("%d", &n);
}
for (int i=0; i<n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main (){
int arr[25];
int n = 25; /* Update thanks to PHIFounder */
CreatePermutation(n, arr);
for (int i=0; i<n; i++)
printf("%d", arr[i]);
}
I will show simple example to make things clearer , you can get a clue from it and apply in your problem .
If you want then you can do it like this :
#include <stdio.h>
int CreatePermutation(int arr[] , int n)
{
for ( int i = 0; i < n; i++)
arr [i] = i+1;
return arr[n-1];
}
int main ()
{
int arr[25];
int n ;
printf ("Please enter number of elements to be printed : ");
scanf ("%d", &n);//You should enter the number of elements before passing it to the function and in this way you can limit the number of elements you want to print .
CreatePermutation ( arr , n );
for (int i = 0; i < n; i++)
printf ("%d\n" , arr[i] );
}
NOTE-- My example shows a simple way to pass array as per your intention , you can further adjust it according to your needs :)