How can I find the sum with the pointers? - c

How can I fix that with pointers ? with *p ?
How can ı find the total of columns?
I want to choose which column ı chose and see the total of it.
lets say array is this:
2 6 9
5 6 9
4 8 4
2 6 0
4 6 7
then here is my program fragment:
for(*p=0;p<size;p++)
sum=sum+*p;
printf("\n");

Using pointers:
#include <stdio.h>
#define Size(x) (sizeof(x) / sizeof(*x))
int main() {
int array[][3] = {
{2, 6, 9},
{5, 6, 9},
{4, 8, 4},
{2, 6, 0},
{4, 6, 7}
};
int selected_column;
// Size(*array) will be 3, the number of columns, that is:
// sizeof(int[3]) / sizeof(int)
printf("Select column (1 - %zu): ", Size(*array));
if(scanf(" %d", &selected_column) != 1 ||
selected_column < 1 || selected_column > Size(*array)) return 1;
--selected_column; // make it zero-based
int sum = 0;
// Size(array) will be 5, the number of rows, that is:
// sizeof(int[5][3]) / sizeof(int[3])
for(int(*p)[Size(*array)] = array, (*e)[Size(*array)] = p + Size(array);
p != e; ++p)
{
sum += (*p)[selected_column];
}
printf("col %d sum = %d\n", selected_column, sum);
}
A much simpler and idiomatic approach would be to iterate over he outer array (consisting of int[3]s):
#include <stdio.h>
#define Size(x) (sizeof(x) / sizeof(*x))
int main() {
int array[][3] = {
{2, 6, 9},
{5, 6, 9},
{4, 8, 4},
{2, 6, 0},
{4, 6, 7}
};
int selected_column;
printf("Select column (1 - %zu): ", Size(*array));
if(scanf(" %d", &selected_column) != 1 ||
selected_column < 1 || selected_column > Size(*array)) return 1;
--selected_column;
int sum = 0;
for(int row = 0; row < Size(array); ++row) {
sum += array[row][selected_column];
}
printf("col %d sum = %d\n", selected_column, sum);
}
Output from both versions:
col 0 sum = 17

Related

How can I reduce the execution time in this code?

Problem
Consider the sequence D of the last decimal digits of the first N Fibonacci numbers, i.e. D = (F0%10,F1%10,…,FN−1%10).
Now, you should perform the following process:
Let D=(D1,D2,…,Dl)
If l=1, the process ends.
Create a new sequence
E=(D2,D4,…,D2⌊l/2⌋)
In other words, E is the sequence created by removing all odd-indexed elements from D
Change D to E
When this process terminates, the sequence D
contains only one number. You have to find this number.
Input
The first line of the input contains a single integer T
denoting the number of test cases.
The description of T test cases follows.
The first and only line of each test case contains a single integer N
Output
For each test case, print a single line containing one integer ― the last remaining number.
Code
#include <stdio.h>
#include <stdlib.h>
int test(int *arr, int n);
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
int *arr;
arr = (int *)malloc((n + 1) * sizeof(int));
arr[1] = 0;
arr[2] = 1;
for (int i = 3; i <= n; i++) {
arr[i] = arr[i - 1] + arr[i - 2];
}
/*
for(int k=1;k<=n;k++){
printf("%d ",arr[k] );
}
printf("\n");
*/
printf("%d\n", (test(arr, n)) % 10);
}
}
int test(int *arr, int n) {
if (n == 1) {
return arr[1];
} else {
for (int i = 1; i <= (n / 2); i++) {
arr[i] = arr[2 * i];
}
return test(arr, n / 2);
}
}
Using the algorithm from https://math.stackexchange.com/questions/681674/recursively-deleting-every-second-element-in-a-list,
Find the largest integer A, such that 2^A < N.
Find Fibonnaci(2^A - 1) % 10
Adding to Bill Lynch's answer, which is itself based on this other answer by happymath:
You will always end up getting 2n − 1 where n is maximum integer such that 2n < K
I'd like to point out another useful mathematical property.
In number theory, the nth Pisano period, written π(n), is the period with which the sequence of Fibonacci numbers taken modulo n repeats.
(https://en.wikipedia.org/wiki/Pisano_period)
Here we need to consider the case where n = 10, π(10) = 60 and the last decimal digits correspond to the OEIS sequence A003893:
0, 1, 1, 2, 3, 5, 8, 3, 1, 4, 5, 9, 4, 3, 7, 0, 7, 7, 4, 1, 5, 6, 1, 7, 8, 5, 3, 8, 1, 9, 0, 9, 9, 8, 7, 5, 2, 7, 9, 6, 5, 1, 6, 7, 3, 0, 3, 3, 6, 9, 5, 4, 9, 3, 2, 5, 7, 2, 9, 1
So that there's no need to calculate the actual Fibonacci number, nor to generate all the sequence up to N.

Every possible path from 2D array? (Cross Product)

Let's suppose I have this array
int diml = 3;
int dims = 3;
int time [diml][dims] ={
(10, 3, 5),
( 4, 7, 2),
( 2, 8, 1)
};
How can I get every combination like:
(10, 3, 5)
(10, 3, 2)
(10, 3, 1)
(10, 7, 5)
(10, 7, 2)
(10, 7, 1)
...
(2, 8, 5)
(2, 8, 2)
(2, 8, 1)
*Is this possible without saving all the combinations in a new array, but just a 1D local array that can store the current combination on every cycle?
*I'd prefer cycles over recursion. And at the end of each cycle I need the pattern (like 10, 3, 2) so I can elaborate it.
*The dimensions of the 2D array are MxN (3x3 is just an example).
*A solution with binary trees is accepted (but I want to save the indexes too).
I should do this in C. I have found similar solutions in StackOverflow but they work by column and they save the data in a 2D array, but that's not what I need.
Thanks in advance! (:
For this example codes, The first one was built so it would be easier to understand example 2. Example 1 was built for only 3x3 matrixes. Example 2 was built so that it can accommodate a matrix with 8 columns at maximum. I didn't use malloc or return an array. It will print back all the possible combinations for you. It doesn't deal with returning the data but it wouldn't be hard to incorporate that into the code.
For the method of calculation all the possible combination, I would use a 3x3 matrix as an example.
In a 3x3 matrix, there are 3 rows and 3 columns. I treated each column as a set of number that I can pick and the rows as the possible of numbers that I can pick from. So in that example, I can pick 012 for my first, second, and third set of number.
So to get all the possible combinations, I have 3 arrays, [0] [1] [2]. They all start at 0. I first save the possible combination of 0 0 0. Then I increase array 2 by 1. Then I would get 0 0 1. I then save that combination. I will keep on doing that and one the [2] array == 2. I turn that to 0 and add a 1 to the array to the left of it. So it become 0 1 0. When I reach a loop where my values of my arrays are 0 2 2, the loop after that, I will get 1 0 0. I will keep on doing that until all the value turn to zero then I am done.
For how the data is store, I store them continually in an array. To read back the value properly. Say for example in a 2x5 matrix. Each combination will have 5 numbers. Thus, the first combination is the first five indexes, the next combination, is the next five numbers after that one.
To calculate how much array length you would need before calculating the combinations, you can just base the calculation on rows and columns. Think of this like the lottery. If there are 3 columns, it like you can pick 3 numbers. Each column have 3 rows, so that mean for each time you pick a number there are 3 possible numbers to pick from. For you to hit a jackpot the chances are 1:3 x 1:3 x 1:3 or 1:27 because there are 27 possibilities if picking 3 numbers like this (123 123 123) and matching them in the correct order. Thus, for a 3x3 matrix, it is 3x3x3, 4x4 = 4x4x4x4, 1x3 = 1, 3x1 = 3, 2x5 = 2x2x2x2x2 = 32.
Thus, the amount of possible combinations is "the amount of rows" to the power of "the amount of columns".
The size is the amount of possible combinations multiply by the amount of numbers per combination. Of which would be "possibilities multiply column count = array size needed.
Example 1:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int m1 = 3;
const int m2 = 3;
int matrix[m1][m2] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};
printMatrixCombo(m1, m2, matrix);
return 0;
}
// Only use this for a 3x3
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int output[81] = {0};
for (int group1 = 0; group1 < 3; group1++){
for (int group2 = 0; group2 < 3; group2++ ){
for (int group3 = 0; group3 < 3; group3++ ){
output[oi++] = matrix[group1][0];
output[oi++] = matrix[group2][1];
output[oi++] = matrix[group3][2];
}
}
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}
Example 2:
#include <stdio.h>
void printMatrixCombo(int row, int col, int matrix[row][col]);
int main(){
const int row = 4;
const int col = 4;
/*// 3x3
int matrix[row][col] = {
{10, 3, 5},
{4, 7, 2},
{2, 8, 1}
};//*/
// 4 x 4
int matrix[row][col] = {
{10, 3, 5, 7},
{4, 7, 2, 3},
{2, 8, 1, 9},
{9, 4, 8, 11}
};//*/
/*// 5 x 5
int matrix[row][col] = {
{10, 3, 5, 7, 25},
{4, 7, 2, 87, 42},
{2, 8, 1, 85, 39},
{9, 4, 8, 94, 57},
{10, 3, 5, 7, 93},
};//*/
/*// 2 x 2
int matrix[row][col] = {
{10, 3},
{4, 7},
};//*/
/*// 1 x 1
int matrix[row][col] = {
{10},
};//*/
/* // 3 x 1
int matrix[row][col] = {
{10},
{4},
{1}
}; //*/
/*// 1 x 3
int matrix[row][col] = {
{10, 4, 1},
};// */
printMatrixCombo(row, col, matrix);
return 0;
}
void printMatrixCombo(int row, int col, int matrix[row][col]){
int oi = 0;
int allZ = 0;
// This is the maximum for a 5x5
// Change to fit usage case
int output[15625] = {0};
int colCount[8] = {0};
int lastCol = col - 1;
int lastRow = row - 1;
while (1){
for ( int i = 0; i < col; i++ )
output[oi++] = matrix[colCount[i]][i];
if ( colCount[lastCol] == lastRow ){
colCount[lastCol] = 0;
for (int i = lastCol - 1; i > -1; i--){
if ( colCount[i] == lastRow ){
colCount[i] = 0;
} else {
colCount[i]++;
break;
}
}
} else {
colCount[lastCol]++;
}
allZ = 1;
for ( int i = 0; i < col; i++ ){
if ( colCount[i] != 0 ){
allZ = 0;
break;
}
}
if (allZ == 1) break;
}
printf("There were %d combination in the matrix of %d x %d\n", oi / col, row, col );
printf("Array's length(indexes) is %d\n", oi );
for ( int i = 0; i < oi ; ){
printf("(");
for ( int j = 0; j < col; j++ ){
printf("%d", output[i+j]);
if ( j != col - 1 ) printf(", ");
}
printf(")\n");
i = i + col;
}
}

Looping through and comparing subsets of arrays

I have two arrays:
int group_id[] = {1, 1, 2, 2, 2, 3, 3, 3};
int value[] = {1, 0, 3, 5, 0, 2, 1, 6};
From the second array, I need to return the largest value within the group_id index (not including the current index position), the result (in a new array) would be:
{0, 1, 5, 3, 5, 6, 6, 2}
The arrays are a lot longer (~10 millions), so looking for an efficient solution.
Clarification:
The first two elements of value belong to group_id = 1, the first element will return 0 as the highest value as it can't return its self. The second element will will return 1 as it's the largest value in group_id 1.
the third, fourth and fifth elements (3, 5, 0) belong to group_id 2, the first will return 5, the second 3 (as it can't return its own index and the third will return 5).
It isn't clear that all the elements in group_id with the same number are adjacent (but that is crucial for efficiency).
Good point, you can assume they are all adjacent.
It isn't clear what should happen if there was only one entry in group_id with a given value — there isn't an alternative entry to use, so what should happen (or should the code abandon ship if the input is invalid).
Assume invalid.
The problem can be solved in O(N) time; it does not need O(N•log N) and sorting. This code shows how:
/* SO 5723-6683 */
#include <assert.h>
#include <stdio.h>
static void dump_array(const char *tag, int size, int *data);
static void test_array(const char *tag, int size, int *groups, int *values);
int main(void)
{
int groups1[] = { 1, 1, 2, 2, 2, 3, 3, 3 };
int values1[] = { 1, 0, 3, 5, 0, 2, 1, 6 };
int groups2[] = { 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5 };
int values2[] = { 1, 1, 3, 5, 0, 2, 1, 6, 6, 3, 5, 5, 5, 3, 2, 3, 7, 3 };
enum { NUM_VALUES1 = sizeof(values1) / sizeof(values1[0]) };
enum { NUM_VALUES2 = sizeof(values2) / sizeof(values2[0]) };
test_array("Test 1", NUM_VALUES1, groups1, values1);
test_array("Test 2", NUM_VALUES2, groups2, values2);
return 0;
}
static void test_array(const char *tag, int size, int *groups, int *values)
{
printf("%s (%d):\n", tag, size);
dump_array("values", size, values);
dump_array("groups", size, groups);
int output[size];
int grp_size;
for (int lo = 0; lo < size - 1; lo += grp_size)
{
assert(groups[lo+0] == groups[lo+1]);
grp_size = 2;
int max_1 = (values[lo+0] < values[lo+1]) ? values[lo+1] : values[lo+0];
int max_2 = (values[lo+0] < values[lo+1]) ? values[lo+0] : values[lo+1];
for (int hi = lo + 2; hi < size && groups[hi] == groups[lo]; hi++)
{
grp_size++;
if (values[hi] >= max_1)
{
max_2 = max_1;
max_1 = values[hi];
}
else if (values[hi] >= max_2)
max_2 = values[hi];
}
for (int i = lo; i < lo + grp_size; i++)
output[i] = (values[i] == max_1) ? max_2 : max_1;
}
dump_array("output", size, output);
}
static void dump_array(const char *tag, int size, int *data)
{
printf("%s (%d):", tag, size);
for (int i = 0; i < size; i++)
printf(" %d", data[i]);
putchar('\n');
}
Output from this test program:
Test 1 (8):
values (8): 1 0 3 5 0 2 1 6
groups (8): 1 1 2 2 2 3 3 3
output (8): 0 1 5 3 5 6 6 2
Test 2 (18):
values (18): 1 1 3 5 0 2 1 6 6 3 5 5 5 3 2 3 7 3
groups (18): 1 1 2 2 2 3 3 3 3 3 4 4 4 5 5 5 5 5
output (18): 1 1 5 3 5 6 6 6 6 6 5 5 5 7 7 7 3 7
The following code will do it. Its efficiency is O(sum of all n_ilog(n_i)) in which n_i is the size of each subset i, unless we use MPI or OpenMP (in that case, it will be at best O(mlog(m)), in which m is the size of the greatest subset).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int compare (const void * e1, const void * e2)
{
int f = *((int*)e1);
int s = *((int*)e2);
return (f>s);
}
int main(int argc, char* argv[])
{
int group_id[] = {1, 1, 2, 2, 2, 3, 3, 3};
int value[] = {1, 0, 3, 5, 0, 2, 1, 6};
int i,j,k,count,*tmp;
for (i=0; i<8; i++)
{
/* find subsets */
count = 1;
for (j=i; j<7 && group_id[j]==group_id[j+1]; j++)
count++;
/* copy subset */
tmp = malloc(sizeof(int)*count);
memcpy(tmp, &value[i], sizeof(int)*count);
/* sort */
qsort (tmp, count, sizeof(*tmp), compare);
/* print */
for (k=i; k<=j; k++)
if (value[k] != tmp[count-1])
printf("%d ", tmp[count-1]);
else
printf("%d ", tmp[count-2]);
i = j;
free(tmp);
}
printf("\n");
return 0;
}
PS: You will probably have to do some modifications to it, but I hope its enough for what you want (or to get you started). Please, be aware, I am assuming each subset has size at least 2, and that the greatest value within a subset appears only once.

C - delete element from array and reorganise

Given this array:
int a[] = {5, 8, 5, 6, 9, 5};
Would it be possible to remove all ints which equals 5 and move the rest the front of the array?
So that after the removal the array would look like this:
int a[] = {8, 6, 9, 0, 0, 0}
I don't know if by removing a element it becomes a 0 or a NULL?
Thanks!
You can do it with two iterations over the array, first iteration two to turn the element you want to delete, second iteration to separate zeros from non-zeros.
int a[] = {5, 8, 5, 6, 9, 5};
int n = 6;
for(int i = 0 ; i < n ; i++ ) {
if(a[i] == 5 ) {
a[i] = 0;
}
}
int* zero = a;
int* nonZero = a;
int j = 0;
while(j < n) {
while(*zero != 0) {
zero++;
}
while(*nonZero == 0) {
nonZero++;
j++;
}
if(zero < nonZero) {
*zero = *nonZero;
*nonZero = 0;
}
j++;
}
Your array is statically allocated, so always has the same size and deleted elements have the 0 value (according how you define the deleted values).
This link can help you and explains about how to delete element from array.
It is been awhile that i have programmed in C but it is posibble.
This is just a pseudo code, but you just need to change it to way of C programming.
int a[] = {5, 8, 5, 6, 9, 5};
int b[] = {5, 8, 5, 6, 9, 5}; // copy of array a to hold temp
for(int i = 0; i < Size of array; i++ ){
for(int j = i; j < Size of array; j++ ){
if(b[j] != 5){
a[i] = b[j];
a[j] = b[i];
break;
}
}
}
It will be like (▼: Target to swap, F: Finished, X: Not a targe to swap, N: Not processed):
▼, ▼, N, N, N, N
5, 8, 5, 6, 9, 5
F, ▼, X, ▼, N, N
8, 5, 5, 6, 9, 5
F, F, ▼, X, ▼, N
8, 6, 5, 5, 9, 5
Result:
8, 6, 9, 5, 5, 5
And remove 5s, it is quite different depends what you mean. If you do not change size of array then they can be 0 or undefined(null). So I think it differs by how you program the function that returns array.
your array is not dynamic so you just can't reduce its size after its been allocated.setting the value zero might solve the problem in your case.

shifting a sequence of numbers in C?

I have a question about some trying to wrap around a sequence of numbers that I'm trying to shift in the C programming language. The first value that is found in the sequence of numbers I calculate via a loop gets thrown out in the end. Here is what the code looks like right now:
numbers[d] = numbers[x];
for (d = y-1; d >=0; d --){
numbers[d] = numbers[(d - 1) % y];
printf(" numbers[d] = %d \n", numbers[d]);
}
Here are the numbers[x] I calculated from my previous loop:
1, 17, 3, 15, 14, 6, 12, 8, 10
Here is what the numbers[d] currently looks like:
17, 3, 15, 14, 6, 12, 8, 10, 10
...and here is what it should look like:
17, 3, 15, 14, 6, 12, 8, 10, 1
It seems like it doesn't wrap the 1 around to the end. Is there a conditional that I am missing in my loop? Thanks!
Let's analyze your for loop, minus the printf statement.
for (d = y-1; d >=0; d --){
numbers[d] = numbers[(d - 1) % y];
}
Before you start the loop, you have the following values in numbers.
1, 17, 3, 15, 14, 6, 12, 8, 10
The value of y is 9.
In the first iteration of the loop, d = 8. (d-1)%y = 7. You replace the value of number[8] by number[7]. The array becomes:
1, 17, 3, 15, 14, 6, 12, 8, 8
In the next iteration of the loop, d = 7. (d-1)%y = 6. You replace the value of number[7] by number[6]. The array becomes:
1, 17, 3, 15, 14, 6, 12, 12, 8
When you reach the iteration where d=1, (d-1)%y = 0. You replace the value of number[1] by number[0]. The array becomes:
1, 1, 17, 3, 15, 14, 6, 12, 8
In the next iteration, d=0, (d-1)%y = -1. The statement
numbers[d] = numbers[(d - 1) % y];
is equivalent to
numbers[0] = numbers[-1];
This certainly leads to undefined behavior but it doesn't explain the other numbers in your output. Maybe the output that you posted corresponds to a different block of code.
I think the answer by #JonathanLeffler gives a solution to your algorithmic problem. I won't repeat that here.
Code
#include <stdio.h>
static const int debug = 0;
static void dump_array(const char *tag, int n, const int array[n])
{
printf("%s (%d)", tag, n);
for (int i = 0; i < n; i++)
printf("%3d", array[i]);
putchar('\n');
}
static void rot1u(int n, int numbers[n])
{
int v = numbers[n-1];
for (int d = n - 1; d >= 0; d--)
{
numbers[d] = numbers[(n + d - 1) % n];
if (debug)
printf(" numbers[%d] = %d\n", d, numbers[d]);
}
numbers[0] = v;
dump_array("Up After: ", n, numbers);
}
static void rot1d(int n, int numbers[n])
{
int v = numbers[0];
for (int d = 0; d < n; d++)
{
numbers[d] = numbers[(d + 1) % n];
if (debug)
printf(" numbers[%d] = %d\n", d, numbers[d]);
}
numbers[n-1] = v;
dump_array("Dn After: ", n, numbers);
}
int main(void)
{
int numbers[] = { 1, 17, 3, 15, 14, 6, 12, 8, 10 };
enum { N_NUMBERS = sizeof(numbers) / sizeof(numbers[0]) };
dump_array("-- Before:", N_NUMBERS, numbers);
rot1u(N_NUMBERS, numbers);
rot1d(N_NUMBERS, numbers);
rot1d(N_NUMBERS, numbers);
rot1d(N_NUMBERS, numbers);
rot1u(N_NUMBERS, numbers);
rot1u(N_NUMBERS, numbers);
return 0;
}
Example output
-- Before: (9) 1 17 3 15 14 6 12 8 10
Up After: (9) 10 1 17 3 15 14 6 12 8
Dn After: (9) 1 17 3 15 14 6 12 8 10
Dn After: (9) 17 3 15 14 6 12 8 10 1
Dn After: (9) 3 15 14 6 12 8 10 1 17
Up After: (9) 17 3 15 14 6 12 8 10 1
Up After: (9) 1 17 3 15 14 6 12 8 10
You need to save the element(s) which should be rotated to the the other side before the loop, and only put it into its proper place (theem into their proper places) afterwards.
#include <stdio.h>
#include <time.h>
#define METHOD 2
#define MAX_SKIERS 20
int starting_lineup[MAX_SKIERS+1];
int main(void)
{
int i, num_skiers = 20;
srand(time(NULL));
int pos1, pos2, temp;
for (i = 0; i <= num_skiers; i++)
starting_lineup[i] = i;
for (i = 0; i < num_skiers*2; i++) {
// Generate two random positions
pos1 = rand() % num_skiers + 1;
pos2 = rand() % num_skiers + 1;
// Swap the skiers at the two positions
temp = starting_lineup[pos1];
starting_lineup[pos1] = starting_lineup[pos2];
starting_lineup[pos2] = temp;
}
printf("The starting lineup (first to last):\n");
for (i = 1; i <= num_skiers; i++)
printf("%s%d", (i == 1 ? "" : ", "), starting_lineup[i]);
putchar('\n');
return 0;
}

Resources