At the moment I'm wondering how can I swap the values already in a two dimensional array, in this case I managed to swap them, however, I can't seem to be able to swap them back... How can I make a program swap the values back and forward for the array?
Code:
#include <stdio.h>
void main()
{
//declaration of values
int d = 0;
char value;
float list[5][6], swaplist[5][6];
float average_odd, sum = 0, average_even, sum1 = 0;
//Command list so that the user knows what he can do in this program
printf("Command list:\t \n\nCommand: \t\t Output: ");
printf("\n \"A\" \t\t Declare values of a list.\n \"O\" \t\t Obtain the average value of the even and odd column\n\t\t values in the list.\n");
printf(" \"I\" \t\t Exchange the values on the even columns with the odd ones.\n \"P\" \t\t Print the values of the list.\n \"S\" \t\t End program.");
//========================================================================================================
while (d != 1)
{
printf("\n\nInsert value: ");
scanf(" %c", &value);
//=========================================================================================================
if (value == 'a' || value == 'A')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("Insert value of element %d in column %d: ", i + 1, j + 1);
scanf("%f", &list[i][j]);
swaplist [i][j] = list[i][j];
}
}
}
//=========================================================================================================
if (value == 's' || value == 'S')
{
d++;
}
//=========================================================================================================
if (value == 'P' || value == 'p')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j++)
{
printf("%2.2f ", list[i][j]);
}
printf("\n");
}
}
//========================================================================================================
if (value == 'o' || value == 'O')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
sum += list[i][j];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
sum1 += list[i][j];
}
}
average_even = sum1 / 15;
printf("The average of the even columns = %.2f\n", average_even);
average_odd = sum / 15;
printf("The average of the odd columns = %.2f\n", average_odd);
}
//=======================================================================================================
if (value == 'i' || value == 'I')
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j+1];
}
}
for (int i = 0; i < 5; i++)
{
for (int j = 1; j < 6; j = j + 2)
{
list[i][j] = swaplist[i][j - 1];
}
}
}
}
}
After swapping, you don't change the state of the swap list. At first, the swap list is a copy of the unswapped matrix. You don't change it to reflect the changes in the matrix and don't keep track of any other state.
You don't really need the swap list. I suggest that you just do a regular swapping of adjacent columns cell by cell:
if (value == 'i' || value == 'I') {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 6; j = j + 2) {
float swap = list[i][j];
list[i][j] = list[i][j + 1];
list[i][j + 1] = swap;
}
}
}
That will toggle the matrix on repeated swaps.
Related
The code fits the first number and prints it constantly. how can i fix this?
int count = 0;
for (int i = 0; i <= 20; i++) {
for (count = 2; i > 1; count++) {
while (i % count == 0) {
printf("%d ", count);
i = i / count;
}
}
}
The values in each iteration are as follows.
count = 0; i = 0; Doesn't enter the second for.
count = 0; i = 1; Doesn't enter the second for.
count = 0; i = 2; Enters the second for. count = 2;
2 % 2 == 0 - Enters the while.
i = 2 / 2; 1 % 2 == 1; Doesn't enter the while.
Back to the second for - count = 3;, i = 1; Doesn't enter the second for.
Back to the first for - i < 20;, so i = 2.
count = 2; i = 2; and we are back to step 4, with an infinite loop.
This might be what you are looking for -
int j, count = 0;
for (int i = 20; i > 0; i--)
{
printf("\n%d: ", i);
for(count = 2, j = i; j > 1; count++)
{
while(j % count == 0)
{
printf("%d ", count);
j = j / count;
}
}
}
Define a function that checks whether a given number n is prime:
bool is_prime(int n)
{
if (n < 2) return false;
for (int i = 2; i <= n/i; ++i) // Doing i*i<=n may overflow
if (n % i == 0) return false;
return true;
}
And then call it like:
for (int i = 0; i <= 20; i++)
if(is_prime(i))
printf("%d\n", i);
Or more directly (i.e. without a function):
int main(void)
{
int mark;
for (int n = 2; n <= 20; n++) {
mark = 1;
for (int i = 2; i*i <= n; ++i)
if (n % i == 0) mark = 0;
if (mark) printf("%d\n", n);
}
}
I have a problem. I need to write a program that checks if every row has the same sum, and if every column has same sum.
Example:
3 3 3
3 3 3
In this example output should be "YES" for rows and "YES" for columns.
Another example:
4 5 6
6 4 5
In this example, output should be "YES" for rows because sum of 1st and 2nd row is the same (15), and it should be "NO" for columns because sum is not the same (10,9,11).
I have made code that checks first row and first column than it compares if they are same as the other ones. I have made it to check if its not but I don't know how to check it if it is, I mean how to output "YES" for both cases.
This is my code:
#include <stdio.h>
int main() {
int mat[100][100];
int n, m;
int i, j;
int sumak = 0;
int sumar = 0;
int sumarp = 0;
int sumakp = 0;
do {
printf("Unesite brojeve M i N: ");
scanf("%d %d", &m, &n);
} while (m < 0 || m > 100 || n < 0 || n > 100);
printf("Unesite clanove matricee: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &mat[i][j]);
}
}
// suma of first row
for (i = 0; i < 1; i++) {
for (j = 0; j < n; j++) {
sumarp = sumarp + mat[i][j];
}
sumarp = sumarp + mat[i][j];
}
// sum of all rows
for (i = 1; i < m; i++) {
for (j = 0; j < n; j++) {
sumar = sumar + mat[i][j];
}
if (sumarp != sumar) {
printf("NE");
} else {
sumar = 0;
continue;
}
}
// sum of the first column
for (j = 0; j<1; j++) {
for (i = 0; i< m;i++ ) {
sumakp = sumakp + mat[i][j];
}
sumakp = sumakp + mat[i][j];
}
// sum of every column
for (j = 1; j < n; j++) {
for (i= 0; i < m; i++) {
sumak = sumak + mat[i][j];
}
if (sumakp == sumak) {
sumak=0;
continue;
}
if(sumakp!=sumak)
{
printf("NE");
return 0;
}
else{
printf("DA");
}
}
}
So if someone can explain me how to do the rest of it.
Thank you!
Use a variable to indicate whether any of the rows or columns didn't match. Then check it at the end of the loop.
Here's how to do it for rows. Columns are similar, just switch the order of the i and j loops.
int all_matched = 1;
for (int i = 0; i < m; i++) {
int sumar = 0;
for (int j = 0; j < n; j++) {
sumar += mat[i][j];
}
if (sumar != sumarp) {
all_matched = 0;
break;
}
}
if (all_matched) {
printf("EQ\n");
} else {
printf("NE\n");
}
I am doing the following problem:
Giving the sequence a consisting of n integer numbers and the sequence b consisting of m integer numbers, two sequences are arranged in increasing order. Combining two above sequences into a new sequence c such that c is also an increasing sequence. Printing c.
Input:
3
1 3 4
4
1 2 3 5
Output:
1 1 2 3 3 4 5
My idea is first combining two sequences into sequence c, then sorting sequence c in increasing order. This is my code:
#include <stdio.h>
int main() {
//Inputting sequence a and b
int n, m;
int a[1001], b[1001];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d", &b[i]);
}
//Combine two sequence into c
int c[1001];
for (int i = 0; i < n; i++) {
c[i] = a[i];
}
for (int i = 0; i < m; i++) {
c[i + n] = b[i];
}
//Arrange sequence c in increasing order
int mid;
for (int i = 0; i < (n + m - 1); i++) {
for (int j = 1; j < (n + m); j++) {
if (c[i] > c[j]) {
mid = c[i];
c[i] = c[j];
c[j] = mid;
}
}
}
//Printing c
for (int i = 0; i < (n + m); i++) {
printf("%d ", c[i]);
}
return 0;
}
However, when I test with the test case [1,2,4],[1,2,5], the result is 1 4 2 2 1 5. Can anyone point out the error in my code? I truly appreciate that.
It is wrong to search for the minimum value in the range of 1 to n+m-1 regardless of the value i. This may move the minimum value to the latter part of the array.
The line
for (int j = 1; j < (n+m); j++)
should be
for (int j = i + 1; j < (n+m); j++)
To merge 2 sorted arrays, you can write 3 loops as in the classic implementation of mergesort:
#include <stdio.h>
int main() {
//Inputting sequence a and b
int a[1000], b[1000], c[2000];
int n, m, i, j, k;
if (scanf("%d", &n) != 1 || n <= 0 || n > 1000)
return 1;
for (i = 0; i < n; i++) {
if (scanf("%d", &a[i]) != 1)
return 1;
}
if (scanf("%d", &m) != 1 || m <= 0 || m > 1000)
return 1;
for (i = 0; i < m; i++) {
if (scanf("%d", &b[i]) != 1)
return 1;
}
//Combine both sequences into c
i = 0, j = 0, k = 0;
while (i < n && j < m) {
if (a[i] <= b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
while (i < n) {
c[k++] = a[i++];
}
while (j < m) {
c[k++] = a[j++];
}
//Printing c
for (i = 0; i < n + m; i++) {
printf("%d ", c[i]);
}
printf("\n");
return 0;
}
I want to display the calendar data in ascending order.
If the date appears several times I need to display it once.
The code works if the date appears five or less times in the input,
if the date appears more than five times at the output it will show up twice.
I don't see the error.
#include <stdio.h>
#include <stdlib.h>
struct date {
int zi;
int luna;
};
int main() {
int n, i, j, k = 0, l;
char c;
scanf("%d", &n);
struct date v[100];
for (i = 0; i < n; i++) {
scanf("%d %c %d", &v[i].zi, &c, &v[i].luna);
}
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (v[i].luna > v[j].luna) {
struct date temp = v[i];
v[i] = v[j];
v[j] = temp;
} else if (v[i].luna == v[j].luna && v[i].zi > v[j].zi) {
struct date temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
}
printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (v[i].luna == v[j].luna && v[i].zi == v[j].zi && i != j) {
printf("\nOK\n");
for (l = j; l < n - 1; l++) {
// v[l].luna=v[l+1].luna;
// v[l].zi=v[l+1].zi;
v[l] = v[l + 1];
}
n--;
}
}
}
for (i = 0; i < n; i++) {
if (v[i].luna < 10 && v[i].zi >= 10) {
printf("%d-0%d\n", v[i].zi, v[i].luna);
} else if (v[i].zi < 10 && v[i].luna >= 10) {
printf("0%d-%d\n", v[i].zi, v[i].luna);
} else if (v[i].zi < 10 && v[i].luna < 10) {
printf("0%d-0%d\n", v[i].zi, v[i].luna);
} else
printf("%d-%d\n", v[i].zi, v[i].luna);
}
}
The problem seems to be this block:
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (v[i].luna == v[j].luna && v[i].zi == v[j].zi && i != j) {
printf("\nOK\n");
for (l = j; l < n - 1; l++) {
// v[l].luna=v[l+1].luna;
// v[l].zi=v[l+1].zi;
v[l] = v[l + 1];
}
n--;
}
}
}
When you remove a duplicate (i.e. by shifting all elements towards the start and decrementing n) you still increment j. Consequently, your loop will skip one element and you may end up with duplicates.
The solution could be as simple as decrementing j at the same time as you decrement n.
BTW: It seems strange that the j-loop start from zero. I would expect it to start from i+1
I'm writing code to check whether each column in my array contains a column full of particular character (z), but there seems to be a flaw in my logic for indicating so. Are there any suggestions for modifying this?
check = 1;
for (i = 1; i < width; i++) {
for (j = 1; j <= column; j++) {
if (!((array[i+1][j] == 'z') && array[i][j] == array[i+1][j])){
check = 0;
printf("No match");
}
}
}
#include <stdio.h>
int main(void){
int width = 3, column = 3;
char array[3][3] = {
{'x','z','y'},
{'b','z','a'},
{'v','z','w'},
};
int i, j, check;
for(j = 0; j < column; j++){
check = 1;
for(i = 0; i < width; i++){
if(array[i][j] != 'z'){
check = 0;
break;
}
}
printf("columns %d is ", j+1);
if(check)
printf("match\n");
else
printf("no match\n");
}
return 0;
}