Code that splits an array and counts repetitions - c

The following code nearly works but for some reason the first value in the array printed is also the last value and also the first value of the next set of data that is printed. I know that might sound confusing.
Here is the code
#include <stdio.h>
#define MAX 288
int nThArrayPrint(double prev, int count, int i, double *array) {
int j = MAX*i;
for (i = j; i <(j+MAX); i++) {
if (array[i] == prev) {
count++;
} else {
printf("%.4lf= %d\t", prev, count);
prev = array[i];
count = 1;
}
}
return count;
}
int main()
{
FILE *fp;
fp = fopen("C:\\Users\\niall\\Desktop\\question2.txt", "r");
double array[MAX * 5]; // Initializes array to all 0.0s
int i, x, j, count;
double prev = 0;
if (fp != NULL) {
for (i = 0; i < MAX * 5; i++) { //Load values from file into an array
fscanf(fp, "%lf", &array[i]);
}
fclose(fp);
prev = array[0]; // initialize
for (i = 0; i < 5; i++){
count = 1;
printf("\tNumber of times each value is repeated for the %dth set of values\n\n", i+1);
count = nThArrayPrint(prev, count, i, array);
prev = array[MAX*i];
printf("%.4lf= %d\n\n", prev, count);
}
} else {
printf("There was a probem opening the file.");
}
}

modify the below segment of the code
prev = array[0]; // initialize
for (i = 0; i < 5; i++){
count = 1;
printf("\tNumber of times each value is repeated for the %dth set of values\n\n", i+1);
count = nThArrayPrint(prev, count, i, array);
prev = array[MAX*i];
printf("%.4lf= %d\n\n", prev, count);
}
into
count = 1;
for (i = 0; i < 5; i++){
printf("\tNumber of times each value is repeated for the %dth set of values\n\n", i+1);
for (j = i*MAX; j < (i*MAX)+MAX; j++) {
if (array[j] == array[j+1]) {
count++;
} else {
printf("%.4lf= %d\n", array[j], count);
count = 1;
}
}
if(count > 1){
printf("%.4lf= %d\n", array[j], count-1);
count = 1;
}
}
Tested with below input with MAX as 9
12 12 12 12 12 1 1 1 1 4 5 5 6 7 8 9 3 3 3 2 2 2 1 1 1 1 1 1 6 62 2.2 2.2 2.2 3.2 3.2 3.2 3.2 22 22 22 22 22 1 22 22 22 22
output:
Number of times each value is repeated for the 1th set of values
12.0000= 5
1.0000= 4
Number of times each value is repeated for the 2th set of values
4.0000= 1
5.0000= 2
6.0000= 1
7.0000= 1
8.0000= 1
9.0000= 1
3.0000= 2
Number of times each value is repeated for the 3th set of values
3.0000= 1
2.0000= 3
1.0000= 5
Number of times each value is repeated for the 4th set of values
1.0000= 1
6.0000= 1
62.0000= 1
2.2000= 3
3.2000= 3
Number of times each value is repeated for the 5th set of values
3.2000= 1
22.0000= 5
1.0000= 1
22.0000= 2

Related

Simple Pyramid In C

the code I've written creates a pyramid with given input. I need the first part Incrementing by 2, but the code written does not really Incrementing by 2. Aprrechiate any help.
Code:
int main() {
int i, j, n, a, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
printf("%d | ", i);
for(a = 2; a <= i; a+=2) {
printf("%d ", a);
}
for (j = 0; j <= i; j++) {
if (j == i){
printf("%d ", j);
}
}
for(n = 1; n < i; n++){
printf("%d ", j);
j++;
}
printf("\n");
}
return 0;
}
Needed Output:
0 | 0
1 | 0 1
2 | 0 2 2 3
3 | 0 2 4 3 4 5
4 | 0 2 4 6 4 5 6 7
5 | 0 2 4 6 8 5 6 7 8 9
6 | 0 2 4 6 8 10 6 7 8 9 10 11
Current Output:
0 | 0
1 | 1
2 | 2 2 3
3 | 2 3 4 5
4 | 2 4 4 5 6 7
5 | 2 4 5 6 7 8 9
6 | 2 4 6 6 7 8 9 10 11
I had to add an extra if condition for i = 0 because it was not fitting in the pattern I figured out. I have done it in just 2 loops instead of you using 3 loops.
Here is the brief explanation. Comment on this answer if you don't understand the code still, I will add a detailed explanation.
Case i = 0 is a special case and it is handled separately using an if.
for i > 0, you can notice that there are 2 series in parallel
even numbers always starting from 0 and if you count them then they are equal to value of i in that row.
another series whose first number is the value of i in that row and it also has number of elements equal to value of i in that row.
That's why in both the loops that handle the series they are run from 0 till i-1.
I hope it helps you and please accept as correct answer if it did.
#include <stdio.h>
int main() {
int i, j, n, a, b, c, rows;
printf("Enter a Number: ");
scanf("%d", &rows);
printf("\nPattern for %d:\n\n", rows);
for (i = 0; i <= rows; i++) {
if(i == 0){
printf("0 | 0\n");
continue;
}
printf("%d | ", i);
// For handling the even number series
for(a = 0, b = 0; a < i; a++, b+=2) {
printf("%d ", b);
}
// For handling the incremental series
for (j = 0, c = i; j < i; j++, c++) {
printf("%d ", c);
}
printf("\n");
}
return 0;
}
You try to overcomplicate simple task.
void pyramid(int rows)
{
for(int row = 0; row < rows; row++)
{
for(int even = 0; even < row * 2; even += 2)
{
printf("%d ", even);
}
if(row == 0) printf("%d ", row);
else
{
for(int remain = row; remain < row * 2; remain++)
{
printf("%d ", remain);
}
}
printf("\n");
}
}
https://godbolt.org/z/cdWqsq

How to print backwards column of matrix represented by array in C?

I am working on program that takes matrix from input file like this. where first row represents parameters of matrix - rows,cols,0 for changing odd cols and 1 for changing even cols
5 5 0
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
and now I have to take odd clomuns (example: 7 4 1) and print it backwards so it will look like this ( 1 4 7 ) if I had 3x3 matrix it would looks like:
1 2 3
4 5 6
7 8 9
output matrix with odd cols printed backwards
7 2 9
4 5 6
1 8 3
and this is code I have so far
int r = 0, c = 0, odds = 0,n = 0,i,j;
FILE *fp,*fp2;
fp = fopen(argv[1],"r");
fp2 = fopen(argv[2],"w");
if(!fp){
printf("file doesnt exist\n");
}
if(!fp2){
printf("file doesnt exist\n");
}
fscanf(fp,"%d %d %d", &r, &c, &odds);
n = r *c;
int* matrix= (int*)malloc(r*c* sizeof(int));
for(int i = 0; i < n; i++)
{
fscanf(fp,"%d",&matrix[i]);
}
if(odds == 0){
for(int i = 0; i < n; i++){
if(i%2==0){
matrix[i] = i;
}
}
}else if(odds == 1){
for(int i = 0; i < n; i++){
if(i%2!=0){
matrix[i] = ;
}
}
}
for(i = 0;i < n; i++)
{
if(i % s == 0 ){
fprintf(fp2,"\n");
}
fprintf(fp2,"%d ",matrix[i]);
}
fclose(fp);
fclose(fp2);
return 0;
}
and my problem is with backwarding the cols, which is supposed to happen here
if(odds == 0){
for(int i = 0; i < n; i++){
if(i%2==0){
matrix[i] = i;
}
}
}else if(odds == 1){
for(int i = 0; i < n; i++){
if(i%2!=0){
matrix[i] = ;
}
}
}
1st if is for printing even cols backwards and 2nd is for odd cols
and as you can see the matrix is in my program represented by normal array
which wasn't my idea, but teachers, so its supposed to work like this
1 2 3|4 5 6|7 8 9 ----> 7 2 9|4 5 6|1 8 3
ok I just found out about map indexing, so now each position in array is presented as matrix[j+(i*r)] so for example 1st position in the 3x3 matrix above would be something like this: matrix[1+(0*3)], 4th pos would be matrix[1+(1*3)] etc...
So now my question is how to index the opposite postion in the column.
code update:
for(int i = 0; i < r; i++){
for(int j = 1; j < c; j++){
if(i%2!=0){
matrix[j+i*r] = ....;
}
}
}
In both instances you want to replace matrix[i] with an element further up in the matrix.
n n
1 2 3
4 5 6
7 8 9
In this example for replacing odd values, 1 and 7 should be switched. The difference in the indices is 6. However this is dependent on the matrix dimensions. The general formula would be:
n^2 - n - r*(2n) + i
where r is the row you are on. Since you are flipping the matrix you only have to do rows up to (n-n%2)/2.
Here is some python code to clarify
for i in range(0,int((matrixdim-matrixdim%2)/2)*matrixdim):
if ((i-r)%2) == 0:
temp = matrix[i]
matrix[i] = matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i]
matrix[matrixdim*matrixdim - matrixdim - r*2*matrixdim + i] = temp
if ((i+1)%matrixdim) == 0:
r += 1
Using i,j notation
for i in range(0,int((matrixdim-matrixdim%2)/2)):
for j in range(0, matrixdim):
pos = j + matrixdim*i
temp = matrix[pos]
if j%2 == even:
matrix[pos] = matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos]
matrix[matrixdim*matrixdim - matrixdim - i*2*matrixdim + pos] = temp

Pattern Printing in C for printing numbers in vertical pattern

I wrote a function to print the below pattern.
For example, if the n value is 4 the pattern is
1
2 7
3 6 8
4 5 9 10
Or if the value of n is 5, then the pattern is
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
My function gives me the first two block but not the next block. I'm stuck here for long time!
My function is
int printPattern(int n) {
int row, column, fwdCtr = 1, evenCtr = 0, ctr = n;
for(row = 1; row <= n; row++) {
fwdCtr = row;
for(column = 1; column <= row; column++) {
if(column % 2 != 0) {
printf("%d ", fwdCtr++);
} else {
evenCtr = fwdCtr + ctr;
printf("%d ", evenCtr);
ctr = ctr - 2;
}
}
printf("\n");
}
}
What I get is
1
2 7
3 6 4
4 5 5 4
Please give suggestions of changes!
The following code should do it:
#include <stdio.h>
void f(int n)
{
for (int i = 0; i < n; ++i)
{
for (int j=0; j<=i; ++j)
{
// Calculate the numbers used so far by previous columns
int x = 0;
for(int v=0; v<j;++v)
{
x = x + (n-v);
}
if ((j % 2) == 0)
{
// even columns
printf("%d ", x+i-j+1);
}
else
{
// odd columns
printf("%d ", x+n-i);
}
}
printf("\n");
}
}
int main(void)
{
f(5);
return 0;
}
Output:
1
2 9
3 8 10
4 7 11 14
5 6 12 13 15
The easy thing to do is just print the right number based on the row and column and the value of n, like this
int main(void)
{
int n = 20;
for (int row = 0; row < n; row++) {
for (int col = 0; col <= row; col++)
printf("%3d ", 1 + col*n - (col-1)*col/2 + (col%2 ? n-1-row : row-col));
printf("\n");
}
}

Print a pattern in C

I wish to print a pattern in C language like this:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
Currently I have this:
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=5;j++)
{
if(i>=j)
{
printf(" %d ",j+i-1);
}
}
printf("\n");
}
printf("\n");
}
I am not getting the desired result.Please can anybody help
Basically if you analyze the difference between numbers at each row:
1
2 6
3 7 10
4 8 11 13
5 9 12 14 15
^ ^ ^ ^
diff 4 3 2 1
Then for each column (except the first one which is equal to the row) the formula is:
col_value = val(row, col-1) + (5-col))
For example, the last row:
5 9 12 14 15
9 = 5 + (5-1)
12 = 9 + (5-2)
14 = 12 + (5-3)
15 = 14 + (5-4)
Code:
#include<stdio.h>
int main()
{
int i,j,k;
for(i=1;i<=5;i++)
{
k = i;
for(j=1;j<=i;j++)
{
printf("%d ", k);
k += 5-j;
}
printf("\n");
}
return 0;
}
Check this :
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
int temp = 4;
int sum = 0;
for(j=1;j<=i;j++)
{
if (j == 1)
sum = i;
else{
sum = sum + temp --;
}
printf("%d ",sum);
}
printf("\n");
}
}
int main () {
int k,i, j;
for (i = 1; i <=5; i++) {
k = i;
for (j = 1; j <= i; j++) {
printf ("%d ", k);
k = k + (5-j);
}
printf ("\n");
}
}
The logic is quite straight forward.
1) The number of elements in a row equals the row number. Hence use the inner loop with j = 1 to j <= i
2) If you see the pattern you observe that every row starts with the number equals to the row index, the next number is +4 and then +3 and so on.
3) Hence use k = k + (5-j)
int main()
{
int i,j,temp=0,l;
for(i=1;i<=5;i++)
{
l=4;
temp = i;
for(j=1;j<=i;j++)
{
if(j>1)
{
printf("%d\t",temp+l);
temp = temp+l;
l=l-1;
}
else
printf("%d\t",i);
}
printf("\n");
}
getch();
return 0;
}

Generating subsets of a set in C(Non recursive)

I am trying to generate all the subsets of a set in a non recursive manner. This is the code:
#include <stdio.h>
main() {
int no_of_element, no_of_subset, i, j, start, index, a[50], x;
printf("Enter the size of main set :");
scanf("%d", &no_of_element);
printf("Enter the elements of main set :");
for (x = 0; x < no_of_element; x++)
scanf("%d", &a[x]);
printf("The subsets are :\n");
for (no_of_subset = 1; no_of_subset <= no_of_element; no_of_subset++) {
for (start = 0; start <= no_of_element - no_of_subset; start++) {
if (no_of_subset == 1)
printf("%d\n", a[start]);
else {
index = start + no_of_subset - 1;
for (j = index; j < no_of_element; j++) {
for (i = start; i < index; i++) {
printf("%d\t", a[i]);
}
printf("%d\n", a[j]);
}
}
}
}
}
This is the output:
Enter the size of main set :4
Enter the elements of main set :1
2
3
4
The subsets are :
1
2
3
4
1 2
1 3
1 4
2 3
2 4
3 4
1 2 3
1 2 4
2 3 4
1 2 3 4
This code isn't generating the subset (1,3,4). Could someone please help me fix it ?
Just for fun, I dit it from scratch.
The trick is to let a counter go through all the possible subsets, each being a combination of its bits.
In case, use unsigned long int to handle sets of up to 64 elements:
#include <stdio.h>
void print_subset(int subset, int *set) {
int pos=0;
printf("Subset %d = ", subset);
while (subset) {
if (subset & 1) {
printf("%d ", set[pos]);
}
subset >>= 1;
pos++;
}
printf("\n");
}
int main()
{
int no_of_element,no_of_subset,x,a[50];
printf("Enter the size of main set :");
scanf("%d",&no_of_element);
printf("Enter the elements of main set :");
for(x=0;x<no_of_element;x++)
scanf("%d",&a[x]);
no_of_subset= (1 << no_of_element);
printf("There are %d subsets\n", no_of_subset);
for (; no_of_subset--;) {
print_subset(no_of_subset, a);
}
}
When run it produces
Enter the size of main set :4
Enter the elements of main set :1
2
3
4
There are 16 subsets
Subset 15 = 1 2 3 4
Subset 14 = 2 3 4
Subset 13 = 1 3 4
Subset 12 = 3 4
Subset 11 = 1 2 4
Subset 10 = 2 4
Subset 9 = 1 4
Subset 8 = 4
Subset 7 = 1 2 3
Subset 6 = 2 3
Subset 5 = 1 3
Subset 4 = 3
Subset 3 = 1 2
Subset 2 = 2
Subset 1 = 1
Subset 0 =
#include <stdio.h>
#include <stdlib.h>
void print(size_t n, int a[n]){
size_t i;
for(i=0; i < n; ++i){
if(i)
putchar(',');
printf("%d", a[i]);
}
putchar('\n');
}
void subset(size_t n, int a[n], size_t size){
int *out = malloc(size * sizeof(*out));
int *level = malloc((size+1)*sizeof(int));
int *index = malloc((size+1)*sizeof(int));
int sp = 0;
level[sp] = 0;
index[sp] = 0;
redo:
while(index[sp] < n){
out[level[sp]] = a[index[sp]];
level[sp+1] = level[sp] + 1;
index[sp+1] = index[sp] + 1;
if(level[++sp] == size){
print(size, out);
--sp;
} else
goto redo;//this means recursive call
++index[sp];
}
if(sp>0){
++index[--sp];
goto redo;
}
free(level);
free(index);
free(out);
}
int main(void){
int a[] = {1, 2, 3, 4};
size_t i, n = sizeof(a)/sizeof(*a);
for(i = 1; i <= n; ++i)
subset(n, a, i);
return 0;
}
result:
1
2
3
4
1,2
1,3
1,4
2,3
2,4
3,4
1,2,3
1,2,4
1,3,4
2,3,4
1,2,3,4

Resources