I want to take integers like that
1 2 3
4 5 6
Than I should sum them with same line like
1+4=5
2+5=7
3+6=9
And I should print 5 7 9.
To do this I tried to take values from user in this way but only first scanf is running and the other is not.What is the reason for that and how can I fix it or is there any other useful way to do that?(How many value will user enter is unknown.)
char s1[21];
scanf("%[^\n]", s1);
char s2[21];
scanf("%[^\n]", s2);
for(int i =0; i<b1; i++)
printf("%d ",s1[i]+s2[i]);
You should read into integer arrays, not strings. scanf() will skip over the whitespace between the numbers.
int numbers1[b1], numbers2[b1];
for (int i = 0; i < b1; i++) {
scanf("%d", &numbers1[i]);
}
for (int i = 0; i < b1; i++) {
scanf("%d", &numbers2[i]);
}
for (int i = 0; i < b1; i++) {
printf("%d ", numbers1[i] + number2[i]);
}
printf("\n");
The following program doesn't work. I should debug it, but I am too lazy for that. However take a look. It might help giving you some idea.
#include <stdio.h>
#define SIZE 50
int main() {
int n[SIZE][SIZE], row, col, sum, i, j;
int numCol; // column of first row
int yeah = 1; // tells wether all the rows have the same number of columns
char c = 'c'; // to check for the newline
for(row = 0; row < SIZE; row++) {
for(col = 0; c != '\n' && col < SIZE; col++) {
if( scanf("%d%c", &n[row][col], &c) < 2 && row > 0 ) {
if( col != numCol - 1) {
yeah = 0;
}
goto outside_pls; // the only way to exit the outer loop;
}
} // end inner for
if( row == 0 ) {
numCol = col;
}
else if( col != numCol ) {
yeah = 0;
}
} // end outer for
outside_pls:
if( yeah ) {
printf("%d %d\n", row, col);
for(j = 0; j < col; j++) {
sum = 0;
for(i = 0; i < row; i++) {
sum += n[i][j];
}
printf("%d ", sum);
}
}
else {
puts("Number of columns doesn't match");
}
return 0;
}
You should take the index you are in and get all of your variables in the same index in all of your arrays.
Code should look like this
for(int i = 0; i< array1.length(); i++){
sum = array1[i]+array2[i]+.......;
}
Related
I am trying to add numbers from 0 to 9 in the row and 0 to 11 in column of a two dimensional array in C. And as for the rest of the empty spaces, I would like to add 0.
The matrix size is 9 x 11. And this is what the output looks like with the empty blocks filled with 0:
And this is the code I have so far but it does not work:
int i;
int j;
int arr[i][j];
int value = 0;
for (i = 0; i < 9; i++){
for (j = 0; j < 11; j++){
arr[i][j] = value;
printf("%d\n", arr[i][j]);
value++;
}
printf("\n");
}
The screenshot that you've posted has 10 rows and 12 columns, so assuming that, here is the code:
int i;
int j;
int arr[10][12];
for (i = 0; i < 10; i++) {
for (j = 0; j < 12; j++) {
if (i == 0) {
arr[0][j] = j;
} else if (j == 0) {
arr[i][0] = i;
} else {
arr[i][j] = 0;
}
printf("%d", arr[i][j]);
}
printf("\n");
}
I am writing a program which determines the intersection of 2 integer arrays (size of 10 elements). I think I got every other parts covered except for sorting out duplicates. Does anyone know a way of checking duplicates without making a function or using an external C library?
#include <stdio.h>
#define SIZE 10
int main(void){
//Initialization
int array1[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set A: ", i + 1);
scanf("%d", &array1[i]);
}
int array2[SIZE];
for (int i = 0; i < SIZE; i++)
{
printf("Input integer %d of set B: ", i + 1);
scanf("%d", &array2[i]);
}
int intersection[SIZE];
for (int i = 0; i < SIZE; i++)
{
intersection[i] = '\0';
}
//Intersection check
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (array1[i] == array2[j])
{
intersection[i] = array1[i];
break;
}
}
}
//duplicate check
int count = SIZE;
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (intersection[i] == intersection[j])
{
for (int k = j; j < count; i++)
{
intersection[k] = intersection[k + 1];
}
count--;
}
}
}
//printing set
for (int i = 0; i < SIZE ; i++)
{
//printf("%d\n", intersection[i]);
if (intersection[i] != '\0')
{
printf("%d\n", intersection[i]);
}
}
return 0;
}
In the code above i was trying one method although it didn't work and instead made the program stuck after inputting all the elements. I am open to other methods as long it doesn't require an external library to run. Thanks
As i see it now , in the third loop where you checking your duplicates i thing that you have to increese k not i :
for (int k = j; j < count; k++), also you must decrise the size of j in your code under the count--;.So your code for checking duplicates seems right but , you want the intersection of this two arrays you made , so you dont have to check for duplicates because in the array intersection[SIZE] you will put only one number from the two arrays, so you will not have duplicates .You should check for duplicates if you wanted to make the union of this two arrays .I make some changings to your code acording what you want to create and this code here find the intersection from two arrays.Try this and delete the duplicate check because that makes your code to run to infinity . One last thing your intersection check must be replace whith this :
//Intersection check
int i = 0, j = 0,k=0; // k is for the intersection array !
while (i < SIZE && j < SIZE) {
if (array1[i] < array2[j])
i++;
else if (array2[j] < array1[i])
j++;
else if(array1[i]==array2[j]) // if array1[i] == array2[j]
{
intersection[k]=array2[j];
//printf("intersection[%d]=%d\n",i,intersection[i]);
intersectCount++;
k++;
i++;
j++;
}
}
printf("intersectCount=%d\n",intersectCount);
I would like to know how to find duplicate values in the 1st row of my 2d array.
I thought that by setting array[0][0] == array[i][j], it would check if the array[0][0] equals to the number of array[0][rest of the column]. But my code is just popping up my try again message whenever I put my first value.
Here's what I've tried so far.
void main(void)
{
int array[2][5];
int i, j, l, k;
printf("\Please enter 10 values\n");
for (j = 0; j < 5; j++)
{
for (i = 0; i < 2; i++)
{
scanf("%i", &array[i][j]);
for (k = 0; k < 2; k++)
{
for (l = 0; l < 5; l++)
{
while (array[0][0] == array[i][j])
{
printf("You entered 2 identical numbers in the first row, try again:\n");
scanf("%i", &array[i][j]);
}
}
}
}
}
}
// this isn't the fastest algorithm but it works because of the small length
int check_duplicates(int arr[], int len) {
// iterate through the array
for (int i = 0; i < len; i++) {
// only need to check to the right
// since the left elements have been checked previously
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
// there's a duplicate, return
return 1;
}
}
}
// no duplicates found
return 0;
}
int main(void) {
int array[2][5];
int i, j, l, k;
printf("Please enter 10 values\n");
for (j = 0; j < 5; j++) {
for (i = 0; i < 2; i++) {
scanf("%i", &array[i][j]);
// a duplicate has been found
if (check_duplicates(array[0], j + 1)) {
printf("You entered a duplicate, try again.\n");
// undo one loop to read back into that position
i --;
}
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024
float RowSum(float **matrix, int sizearray,int row) {
float sum;
for (int j=0 ; j <= row; j++) {
sum = 0;
for (int i=0 ; i < sizearray; i++) {
sum = sum + matrix[j][i];
}
}
return sum;
}
float ColSum(float **matrix, int sizearray, int col) {
float sum;
for (int j = 0; j <= col; j++) {
sum = 0;
for (int i = 0; i < sizearray; i++) {
sum = sum + matrix[i][j];
}
}
return sum;
}
int RepNum(int arraysize, float **matrix) {
int i, j, counter = 0;
int temparray[N_MAX*N_MAX];
for (i = 0; i < N_MAX*N_MAX; i++) {
temparray[i] = 0;
}
for (i = 0; i < arraysize; i++) {
for (j = 0; j < arraysize; j++) {
temparray[(int)matrix[i][j]]++;
}
}
for (i = 1; i < arraysize*arraysize; i++) {
if (temparray[i] > 1)
counter++;
}
return counter;
}
void PrintArray(float **matrix, int arraysize) {
for (int i = 0; i<arraysize; i++)
{
for (int j = 0; j<arraysize; j++)
printf("%3d ", (int)matrix[i][j]);
printf("\n");
}
}
int CheckInt(float **matrix, int arraysize) {
int counter = 0;
for (int i = 0; i < arraysize; i++) {
for (int j = 0; j < arraysize; j++) {
if (((int)matrix[i][j]) != matrix[i][j])
counter++;
}
}
return counter;
}
void main() {
printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
float **matrix;
char input[TEMPSIZE];
int sizear = 0;
float correctsum = 0.0;
int counter = 0, row, column;
fgets(input, TEMPSIZE, stdin);
char* str = strstr(input, " ");
if (str == 0)
return;
str[0] = 0;
str++;
sizear = atof(input);
if (sizear > N_MAX || sizear < N_MIN)
{
printf("ERROR: The matrix size cannot be %d size. \n", sizear);
exit(0);
}
matrix = (float**)calloc(1, sizear * sizeof(float*));
for (column = 0; column < sizear; column++)
{
matrix[column] = (float*)calloc(1, sizear * sizeof(float));
}
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
char* temp = strstr(str, " ");
if (temp == 0) /*gets the last number*/
{
++counter;
matrix[row][column] = atof(str);
goto end;
}
if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
temp[0] = 0;
matrix[row][column] = atof(str);
str = temp + 1;
++counter;
}
else {
printf("you cannot enter the number %.3f \n", atof(temp));
exit(0);
}
}
}
end:
if (counter > sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter < sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter == sizear*sizear) {
correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
float row = RowSum(matrix, sizear, 0);
float coul = ColSum(matrix, sizear, 0);
if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
printf("It's a magic matrix!\n");
PrintArray(matrix, sizear);
}
else {
printf("Not a magic square:\n");
if (row != coul && row != correctsum && coul != correctsum) {
printf("* Coloums and rows sum do not match.\n");
}
if (RepNum(sizear, matrix) != 0) {
printf("* There are repeating numbers.\n");
}
if (CheckInt(matrix, sizear) != 0) {
printf("* One of the numbers or more you've entered isn't integer.\n");
}
}
}
for (column = 0; column < sizear; column++)
{
free(matrix[column]);
}
free(matrix);
}
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
Sure, it's simple: the counter is incremented in the loops
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
…
}
}
- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.
Today I got a irritating question in a coding contest. I creamed through the first round but in the second round the following question got me in a trap.
Question: Input N = 4
Output:
1
0 1
1 0 1
0 1 0 1
I tried many things but every time I failed.
Apart from this stupid wrong solution I tried many fancy stuff and failed in the end.
What part of my C knowledge is weak?
If you were given this question how would you solve it?
Seems pretty trivial to me:
int main(int argc, char *argv[])
{
int n = strtol(argv[1], NULL, 10);
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
printf("%d ", i % 2 ? j % 2 : 1 - j % 2);
}
printf("\n");
}
return 0;
}
You panicked. Since the values you print depend on the row and column, use both. And name your variables better.
void printBinaryTriangle(const unsigned int rows) {
for(int row = 1; row <= rows; ++row) {
for (int column = 0; column < row; ++column) {
printf("%d ", (row + column) % 2);
}
putchar('\n');
}
}
This did the job for me.
int i,j,flag;
int num=4;
flag=1;
for(i=0;i<num;i++)
{
for(j=0;j<i+1;j++)
{
printf("%d",(j+flag)%2);
}
if(flag)
{
flag=0;
}
else
{
flag=1;
}
printf("\n");
}
Is this what you want?
for(int k = 0; k < N; k++){
for(int i = 0; i < k+1; i++){
if(i % 2 == k % 2)
printf("1 ");
else
printf("0 ");
};
printf("\n");
}
num = 12
new_string = ''
new_list = ''
while num:
for i in range(1,num+1):
strings_of_ones = new_string+ '1' * i
new_list = list(strings_of_ones)
for j in range(1,len(new_list),2):
new_list[j]='0'
print(' '.join(new_list[::-1]))
num -= 1
Answer in Java:-
public class practice {
public static void main(String args[]) {
int n=4;
for (int i=1 ;i<=n ; i++) {
for (int j=1 ;j<=i ;j++) {
int a=i+j;
if (a % 2==0) {
System.out.print("1"+" ");
} else {
System.out.print("0"+" ");
}
}
System.out.println();
}
}
}