3x3 array = 10 numbers - c

i have this code
#include <math.h>
#include <stdio.h>
const int n = 3;
const int s = 3;
int getm(int mat[n][s]);
int printm(int mat[n][s]);
int main()
{
int m[n][s];
getm(m);
printm(m);
return 0;
}
int getm(int mat[n][s])
{
for(int x = 0;x < n;x++)
{
for (int y = 0;y<s;y++)
{
scanf("%i ", &mat[x][y]);
}
}
return 0;
}
int printm(int mat[n][s])
{
for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
if(y==(s-1))
{
printf("\n");
}
}
}
}
which shoud ask for 9 numbers to make a 3x3 matrix array, but it actually asks for 10 numbers, printm is working well - printing only 9 numbers. Where is error?

I think the problem is the space after %i: you do not need the tenth number, but your code is asking for it anyway, because it waits to get a space after the ninth number.
Separately, your printing code can be optimized a little by dropping the if:
for(int x = 0;x<n;x++)
{
for(int y = 0;y<s;y++)
{
printf("%i ", mat[x][y]);
}
printf("\n");
}

scanf("%i ", &mat[x][y]);
Get rid of the space after %i, so it only reads a number:
scanf("%i", &mat[x][y]);

Related

How to put commas on a series of numbers in a proper way

Yes I have searched through the net and also here but failed to find similar cases. I have here a program which prints prime numbers between 2 given integers. But I have to print them with commas in a proper way. like 1, 2, 3, 4 without having a comma on the last integer. Here is my code.
#include<stdio.h>
void main()
{
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
for(i=x; i<=y; i++)
{
for(j=2; j<=i; j++)
{if(i%j==0)
{
break;
}
}
if(i==j)
{
printf("%d, ", i);
}
}
}
I wanted to identify the total number of prime numbers printed in order to set a condition that if it is the last one, a comma will not print but I don't know if it will work. That is the only thing I can think of for now and your help guys will be really appreciated. Thank you!
As #kaylum pointed out, use a flag to "skip" printing the comma when printing the first number. Set that flag to false after the very first number you print.
#include <stdio.h>
void main() {
int x, y, i, j;
puts("Enter first number: ");
scanf("%d", &x);
puts("Enter second number: ");
scanf("%d", &y);
int is_first_time = 1;
for (i = x; i <= y; i++) {
for (j = 2; j <= i; j++) {
if (i % j == 0) {
break;
}
}
if (i == j) {
if (is_first_time) {
printf("%d", i);
is_first_time = 0;
}
else {
printf(", %d", i);
}
}
}
}
You can use a flag, as others have suggested, but you can also use a string as a separator, setting it to empty for the first element and any desired separator after that.
#include <stdio.h>
void print_join(const char *sep, int n, int a[n])
{
const char *s = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", s, i);
s = sep;
}
printf("\n");
}
int main(void)
{
int a[] = {1, 2, 3, 4, 5};
int n = sizeof a / sizeof *a;
print_join(", ", n, a);
print_join(";", n, a);
print_join("", n, a);
print_join("--", n, a);
return 0;
}

Do we have to create an even and an odd array?

How can we modify the following code (which initially asks the user for 10 numbers to be entered, get stored in an array, and printed on the screen) so that the even numbers are printed on the first line, and the odd on the second:
#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];
int main() {
for(i=0;i<10;i++) {
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The elements of the array are: ");
for (j=0;j<10;j++) {
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
O(n) Solution:
you have to add odd numbers at the back of the array and add the even numbers at the front of the array and keep track of the indexes.
int array_1[10];
int main() {
int even = 0, odd = 10;
for (int i = 0; i < 10; i++) {
printf("Enter a number: ");
int inp;
scanf("%d", &inp);
if (inp % 2 == 0) {
array_1[even++] = inp;
} else {
array_1[--odd] = inp;
}
}
// even numbers
for (int i = 0; i < even; i++) {
printf("%i ", array_1[i]);
}
printf("\n");
// odd numbers
for (int i = 9; i >= odd; i--) {
printf("%i ", array_1[i]);
}
printf("\n");
return 0;
}
Since you asked, here is how I would do it. I suspect this may leave you with more questions than answers through.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#define NUMBERS_SIZE 10
typedef bool (*number_validator)(int num);
bool isEven(int num)
{
return (num & 1) == 0;
}
bool isOdd(int num)
{
return (num & 1) != 0;
}
void print(const char *title, int *array, int array_size, number_validator isValid)
{
printf("%s", title);
bool first = true;
for (int i = 0; i < array_size; ++i)
{
if (isValid(array[i]))
{
if (!first)
{
printf(", ");
}
printf("%d", array[i]);
first = false;
}
}
printf("\n");
}
int main()
{
int numbers[NUMBERS_SIZE] = { 0 };
for (int i = 0; i < NUMBERS_SIZE; i++)
{
printf("Enter a number: ");
scanf("%d", &numbers[i]);
}
printf("\n");
print("Even: ", numbers, NUMBERS_SIZE, isEven);
print(" Odd: ", numbers, NUMBERS_SIZE, isOdd);
return 0;
}
Demo on ideone
you can try this way.I have used binary AND(&) instead of MOD(%) as it is faster:
#include <stdlib.h>
#include <stdio.h>
int i,j;
int array_1[10];
int main()
{
for(i=0; i<10; i++)
{
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The Even elements of the array are: ");
for (j=0; j<10; j++)
{
if((array_1[j]&1) == 0)
printf("%d ", array_1[j]);
}
printf("\nThe Odd elements of the array are: ");
for (j=0; j<10; j++)
{
if((array_1[j]&1) != 0)
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
No need to create a new array. You can just go through it first checking for even numbers, and then again for odd numbers. Also, there's no need to declare i and j before using them. You can just declare them and initialize them in the for loop:
#include <stdlib.h>
#include <stdio.h>
int array_1[10];
int main() {
for(int i=0;i<10;i++) {
printf("Enter a number: ");
scanf("%d", &array_1[i]);
}
printf("The elements of the array are: ");
// Print even numbers
for (int j=0;j<10;j++) {
if(array_1[j] % 2 == 0)
printf("%d ", array_1[j]);
}
printf("\n");
// Print odd numbers
for (int j=0;j<10;j++) {
if(array_1[j] % 2 != 0)
printf("%d ", array_1[j]);
}
printf("\n");
return 0;
}
Edit: As tadman suggested in the comment below, there's a better and cleaner way to do this kind of task. As you can see in the above example, I'm repeating 4 lines of code where only one character changes. This task could be abstracted into a function to reduce code repetition:
void printIfMod(int* arr, size_t array_size, int mod){
for (int j=0;j<array_size;j++) {
if(arr[j] % 2 != mod)
continue;
printf("%d ", arr[j]);
}
printf("\n");
Remember to add a function prototype before main if you place the function after main:
void printIfMod(int* arr, size_t array_size, int mod);
int main(){...}
Now, to print the numbers, call the method with modulo 0 to get even numbers, and 1 to get odd numbers:
// Print even numbers
void printIfMod(&array_1, 10, 0);
// Print odd numbers
void printIfMod(&array_1, 10, 1);
One last note, hard-coding array_size is not wise, and that goes for all arrays. I recommend using sizeof() to dynamically calculate the size of your array:
size_t size = sizeof(array_1) / sizeof(int);
// Print even numbers
void printIfMod(&array_1, size, 0);
// Print odd numbers
void printIfMod(&array_1, size, 1);

C programming - Incorrect output of matches

The task is to enter the dimension and data into a two dimensional array and determine the number of non repeating numbers. I tried many things so i asked you to give me your instructions or hints(wanna instructions) how to correct my code.
#include <stdio.h>
#include <stdlib.h>
int protect(int max_size_1 ,int max_size_2 , int current_1,int current_2,const int arr[max_size_1][max_size_2])
{
for(int f1=0;f1<max_size_1;f1++)
{
for(int f2=0;f2<max_size_2;f2++)
{
printf("%d---%d\n",arr[current_1][current_2],arr[f1][f2]);
if((f1!=current_1 && f2!=current_2) && arr[current_1][current_2]==arr[f1][f2])
{
return 0;
}
}
}
return 1;
}
int main()
{
int i, j, f1, f2, count=1;
printf("[i][j] = ");
scanf("%d", &i);
int arr[i][i];
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
printf("a[%d][%d] = ", f1, f2);
scanf("%d", &arr[f1][f2]);
}
}
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
if (protect(i,j,f1,f2,arr)==1){
count++;}
}
}
printf("\n\n%d", count/2);
return 0;
}
I believe, you are trying to store data in a 2 dimensional array.
and in next step for every value in 2 dim array, trying to find whether there is any dup entry present in array. No sure of your requirement to have data stored in 2 dim array, also there are other optimized way of removing duplicates from user inputs.
we have n user inputs
want to print the number of entries which do not have a dup in data entered by user.
assume,
if user input is
1, 1
1, 1
answer should be 0.
for input
1, 2
3, 4
answer should be 4.
could see three issues in the code
you have two variables "i" and "j", only one of this var is getting a value assigned , j is always 0. this makes the "protect" function to return a 1 as output for very call.
count is started from 1, should be 0
result is getting devided by 2, not sure why this is required
#include <stdio.h>
#include <stdlib.h>
int protect(int max_size_1 ,int max_size_2 , int current_1,int current_2,const int arr[max_size_1][max_size_2])
{
for(int f1=0;f1<max_size_1;f1++)
{
for(int f2=0;f2<max_size_2;f2++)
{
printf("%d---%d\n",arr[current_1][current_2],arr[f1][f2]);
if((f1!=current_1 && f2!=current_2) && arr[current_1][current_2]==arr[f1][f2])
{
return 0;
}
}
}
return 1;
}
int main()
{
int i, j, f1, f2, count=0;
printf("[i][j] = ");
scanf("%d", &i);
int arr[i][i];
j = i;
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
printf("a[%d][%d] = ", f1, f2);
scanf("%d", &arr[f1][f2]);
}
}
for (f1 = 0; f1<i; f1++)
{
for (f2 = 0; f2<i; f2++)
{
if (protect(i,j,f1,f2,arr)==1){
count++;
}
}
}
printf("\n\n%d\n", count);
return 0;
}

Functions and arrays which allows you to print what's the most repeated number

I have a problem with my code, it doesn't print the result I expect. This code allows the user to enter as many numbers as he wishes and then print the most repeated one
Here it is:
#include <stdio.h>
void reading_numbers(int array[]){
int i = 0;
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
while (i < Max) {
printf("Insert the numbers\n");
scanf("%d", &array[i]);
i++;
}
}
void most_present_number(int array[], int Max){
int i = 0;
reading_numbers(array);
int current_number = array[i];
int current_number_count = 0;
int most_present_one = 0;
int most_present_one_counter = 0;
while (i < Max) {
if (array[i] == current_number) {
current_number_count++;
i++;
} else {
if (current_number_count > most_present_one_counter){
most_present_one = current_number;
most_present_one_counter = current_number_count;
}
current_number_count = 1;
}
}
printf("This is the most present number %d it is repeated %d times\n", most_present_one,
most_present_one_counter);
}
int main() {
int Max = 0;
int array[Max];
most_present_number(array, Max);
return 0;
}
The problem for me is when I call the function, but I don't know how to fix it
I should have written as a premise but I'm a bit new to C so probably there are some things in this code that don't make sense
I make a procedure to find the result ,int main() must have the size of array (mistake logic),because the procedure take the parameters of the main function (int main ())
Here is my code:
#include<stdio.h>
void most_present_number(int Max,int T[Max])
{
int i = 0;
while (i < Max)
{
printf("Insert the numbers :");
scanf("%d", &T[i]);
i++;
}
int k=0,cpt1=0,cpt=0;
for(int i=0;i<Max;i++)
{
cpt=0;
for(int j=i+1;j<Max;j++)
{
if(T[i]==T[j])
{
cpt++;
}
}
if(cpt>=cpt1)
{
cpt1=cpt;
k=T[i];
}
}
printf("This is the most present number %d it is repeated %d times\n",k,cpt1+1);
}
int main()
{
int Max = 0;
do
{
printf("How much long the array will be?\n");
scanf("%d", &Max);
}while(Max<1);
int T[Max];
most_present_number(Max,T);
}
the following proposed code:
cleanly compiles
performs the desired functionality
only includes header files those contents are actually used
and now the proposed code:
#include <stdio.h>
void reading_numbers( int Max, int array[ Max ][2])
{
for( int i = 0; i < Max; i++ )
{
printf("Insert the numbers\n");
scanf("%d", &array[i][0]);
array[i][1] = 0;
}
}
void most_present_number( int Max, int array[ Max ][2] )
{
for( int i=0; i < Max; i++ )
{
for( int j=i; j<Max; j++ )
{
if ( array[i][0] == array[j][0] )
{
array[i][1]++;
}
}
}
int most_present_one = array[0][0];
int most_present_one_counter = array[0][1];
for( int i=1; i<Max; i++ )
{
if( most_present_one_counter < array[i][1] )
{
most_present_one = array[i][0];
most_present_one_counter = array[i][1];
}
}
printf("This is the most present number %d it is repeated %d times\n",
most_present_one,
most_present_one_counter);
}
int main( void )
{
int Max = 0;
printf("How much long the array will be?\n");
scanf("%d", &Max);
int array[Max][2]; // uses variable length array feature of C
reading_numbers( Max, array );
most_present_number( Max, array );
return 0;
}
a typical run of the code:
How much long the array will be?
4
Insert the numbers
1
Insert the numbers
2
Insert the numbers
3
Insert the numbers
2
This is the most present number 2 it is repeated 2 times

C Segmentation Fault With Printf() and Scanf()

I'm new to C, and I'm getting a segmentation fault that I can't understand. I have the following program, which attempts to calculates the number of factors of a strictly positive number:
#include <stdio.h>
#include <math.h>
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
int check (int x) {
if (x>0) {
return 1;
} else {
return 0;
}
}
int main(void) {
int number;
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
}
return 0;
}
The segmentation fault occurs immediately after entering an integer and ENTER after these lines in main():
printf("Please enter a positive integer n such that n >= 1: ");
scanf("%d", &number);
What in these lines is causing the segmentation fault, and what can I do to avoid it?
Your recursion doesn't stop if you try to divide out factor one.
Just let factor never be 1:
for (i=2; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
I should say WHY it segfaults: It's because every function call pushes the current program counter (the position in your program where you currently stand) and function arguments on the stack (aka call stack), where the stack is a relatively small memory block used for, well, function calling and local variables.
So if you are pushing your stack too hard, it will fall over. End of game, aka segfault ;)
You probably have a problem with this recursion:
int numberOfFactors (int number, int factor) {
if (number % factor == 0) {
number = number/factor;
return numberOfFactors(number, factor) + 1;
} else {
return 0;
}
}
Change your numberOfFactors to something like:
int numberOfFactors (int number)
{
int i=1;
int ret=0;
for(;i<=number;i++) {
if (number%i == 0) {
ret++;
}
}
return ret;
}
And then, change this portion:
if (check(number)){
int i;
for (i=1; i<=number; i++) {
int factors;
factors = numberOfFactors(number, i);
printf("%d^%d ", i, factors);
}
}
To something simpler like:
if (check(number)){
factors = numberOfFactors(number);
printf("%d^%d ", number, factors);
}

Resources