Function could replace first n elements from array A with last n elements of array B.
Output is
Array A
1
2
3
4
5
Array B
6
7
8
9
10
I tried to put printf (i) in every loop and it seems to be work fine but it do nothing with arrays a and b :(
void reparray(int *a, int *b, int n){
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i<n; i++){
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element>=n; last_element--){
help[last_element] = b[last_element];
}
i = 0;
for (i; i<n; i++){
a[i] = help[i];
}
last_element = 4;
for (last_element;last_element >= n; last_element--){
b[last_element] = help[last_element];
}
}
int main()
{
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {6, 7, 8, 9, 10};
int n,i;
reparray(a, b, 2);
printf("Array A\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", a[i]);
}
printf("Array B\n");
i = 0;
for (i; i<5; i++){
printf("%d\n", b[i]);
}
return 0;
}
your 2 last loops in void reparray do nothing except copying exact things which were already in a and b
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4-1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
i = 0;
for (i; i< n; i++) {
b[i] = help[i];
}
}
for other input change void reparray
void reparray(int* a, int* b, int n) {
int i;
int last_element;
int help[5] = {};
i = 0;
for (i; i < n; i++) {
help[i] = a[i];
}
last_element = 4;
for (last_element; last_element >= n; last_element--) {
help[last_element] = b[last_element];
}
last_element = 4 - 1;
i = 0;
for (i = 0; i < n; i++) {
a[i] = help[last_element];
last_element++;
}
last_element = 4;
int first_element = 0;
for (i = last_element - n + 1; i < last_element + 1; i++) {
b[i] = help[first_element];
first_element++;
}
}
Related
Here is my code. While choosing values, if I try to put 9 values it dumps garbage value. It has happened while doing quick sort as well
#include <stdio.h>
void printArray(int* A, int n) {
for (int i = 0; i < n; i++) {
printf("%d ", A[i]);
}
printf("\n");
}
int maximum(int A[],int n) {
int i, max = A[0];
for (i = 1; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}
void countSort(int A[], int n) {
int i, max = maximum(A, n);
int count[max + 1], B[n];
for (i = 0; i < max + 1; i++) {
count[i] = 0;
}
for (i = 0; i < max + 1; i++) {
count[A[i]]++;
}
for (i = 1; i < n; i++) {
count[i] += count[i - 1];
}
for (i = n - 1; i >= 0; i--) {
B[--count[A[i]]] = A[i];
}
for (i = 0; i < n; i++) {
A[i] = B[i];
}
}
int main(){
int A[] = {1, 4, 6, 2, 3, 2, 3, 2, 7};
int n = 9;
printArray(A, n); // Printing the array before sorting
countSort(A, n); // Function to sort the array
printArray(A, n); // Printing the array before sorting
return 0;
}
This code uses the wrong limit:
for(i=0;i<max+1;i++) {
count[A[i]]++;
}
It effectively iterates through the elements of A, which has n elements, not max+1.
#include<stdio.h>
void multiplyTwoMatrices(int (*)[2], int[][2], int[][2]);
void copyMatrix(int[][2], int[][2]);
void powerAMatrix(int[][2], int[][2], int);
int main()
{
int num;
scanf("%d", &num);
int i;
for(i = -1; i <= num; i++)
{
printf("\n%d", fib(i));
}
}
int fib(int num)
{
if(num <= 0)
{
return 0;
}
else
{
int matrix[2][2] = {{1, 1}, {1, 0}};
//int fibMatrix[2][2] = powerAMatrix(matrix[2][2], num);
int fibMatrix[2][2];
powerAMatrix(fibMatrix, matrix, num);
return getFibNum(fibMatrix);
}
}
void powerAMatrix(int fibMatrix[2][2], int matrix[2][2], int num)
{
//fibMatrix = matrix;
copyMatrix(fibMatrix, matrix);
int i = 0;
for(i = 1; i < num; i++)
{
//fibMatrix = fibMatrix * matrix;
multiplyTwoMatrices(fibMatrix, fibMatrix, matrix);
}
}
void copyMatrix(int destinationMatrix[2][2], int sourceMatrix[2][2])
{
int i = 0; int j = 0;
for(i = 0; i < 2; i++)
for(j = 0; j < 2; j++)
destinationMatrix[i][j] = sourceMatrix[i][j];
}
void multiplyTwoMatrices(int multipliedMatrix[2][2], int matrixA[2][2], int matrixB[2][2])
{
int i = 0; int j = 0; int k = 0;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
multipliedMatrix[i][j] = 0; //or just initialize it as a zero matrix.
for(k = 0; k < 2; k++)
{
multipliedMatrix[i][j] += (matrixA[i][k] * matrixB[k][j]);
}
}
}
//working alternative
/*
int x = matrixA[0][0]*matrixB[0][0] + matrixA[0][1]*matrixB[1][0];
int y = matrixA[0][0]*matrixB[0][1] + matrixA[0][1]*matrixB[1][1];
int z = matrixA[1][0]*matrixB[0][0] + matrixA[1][1]*matrixB[1][0];
int w = matrixA[1][0]*matrixB[0][1] + matrixA[1][1]*matrixB[1][1];
multipliedMatrix[0][0] = x;
multipliedMatrix[0][1] = y;
multipliedMatrix[1][0] = z;
multipliedMatrix[1][1] = w;
*/
}
int getFibNum(int fibMatrix[2][2])
{
return fibMatrix[0][1];
}
The function multiplyTwoMatrices() seems to work only if "working alternative" (code closed in comments inside this function) is used instead of the one used currently. I'm unable to understand what is going wrong with this code. A little help is appreciated.
Output expected: 0 0 1 1 2 3 5 8 ...
Output coming: 0 0 1 1 1 1 1 1 ...
while your multiplication function is correct, it doesn't work correctly when the destination is one of the operands; if it is, the operand is changed there while the calculation is underway.
Either require that multipliedMatrix is distinct from both matrixA and matrixB (that'd be preferred), or have a temporary matrix there and copy it into the result!
P.S. it would be much easier wrapping the matrix class into a struct:
struct intmatrix2x2 {
int values[2][2];
};
this would make implicit copies when calling functions; and instead of copyMatrix you can say:
struct intmatrix2x2 b = a;
and your multiplication could read as
struct intmatrix2x2 multiply(struct intmatrix2x2 a, struct intmatrix2x2 b)
{
struct intmatrix2x2 result;
int i = 0; int j = 0; int k = 0;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
result.values[i][j] = 0; //or just initialize it as a zero matrix.
for(k = 0; k < 2; k++)
{
result.values[i][j] += a.values[i][k] * b.values[k][j]);
}
}
}
return result;
}
and you could use it as
struct intmatrix2x2 result = multiply(a, b);
Well i m struggling with the pointers, why doesn t work the function biggest in the end (yes of that s dummy function)? (exit code 6)
Ty for help
code:
int search(int const a[], int n, int key) {
for (int *i = a; i < a + n; i++) {
if ( key == *i ) return 1;
}
return 0;
}
void print_row(int const a[], int n, int row) {
for (int *i = a + n * row; i < a + n * (row + 1); i++) {
printf("%d ", *i);
}
printf("\n");
}
void biggest(double x, long *int_part, double *frac_part) {
*int_part = (long) x;
*frac_part = x - *int_part;
}
main () {
int tempretures[7][24];
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
tempretures[6][5] = 32;
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 24; j++) {
printf("%d ", tempretures[i][j]);
}
printf("\n");
}
printf("Is it: %d\n", search(tempretures, 7*24, 32));
for (int i = 0; i < 7; i++) {
print_row(tempretures, 24, i);
}
long a = 0; double b = 0;
biggest(5.67, &a, &b);
printf("%li", a);
}
for (int *i = &tempretures[0][0]; i < &tempretures[7][24]; i++) {
static int j = 1;
*i = j;
j+=2;
}
This runs way past the end of the array, stopping only when it hits every element of row number 7, but there is no row number 7.
I'm using Linux to implement this sorting. If I have a array arr[] ={1, 1, 1, 2, 2, 3, 3}, how to sort it by this:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 1
arr[4] = 2
arr[5] = 3
arr[6] = 1
I Tried to do something like this:
for (int i = 0; i < size; i++)
{
if (arr[i] < arr[i+1])
{
}
}
Please give me some suggestions, thank you so much!
a hint
first you have to sort the array, then starting from 1 to (end-1) if active item equals to the last item move it to the end.
#include <stdio.h>
#include <stdlib.h>
/*__________________________________________________
*/
static int __cdecl sortCallback(int *i1,int *i2){
return (*i1<*i2)?-1:(*i1>*i2)?1:0;
}
/*__________________________________________________
*/
void printArr(char* Title,int *arr,int n){
int i;
if(Title)
printf("%s:\n\t",Title);
for (i=0;i<n;i++)
printf("%d ",arr[i]);
printf("\n");
return;
}
/*__________________________________________________
*/
void arrange(int *arr,int n){
int i=1,j;
int a;
while(i<(n-1)){
if(arr[i]==arr[i-1]){
a=arr[i];
for(j=i;j<(n+1);j++){
arr[j]=arr[j+1];
}
arr[n-1]=a;
}else
i++;
}
}
/*__________________________________________________
*/
int main(void){
int arr[7];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 1;
arr[4] = 2;
arr[5] = 3;
arr[6] = 1;
printArr("Initial",arr,7);
qsort(arr,7,sizeof(int),sortCallback);
printArr("Sorted",arr,7);
arrange(arr,7);
printArr("Rearranged",arr,7);
return 0;
}
#include <stdio.h>
void cnv(int n, int arr[n]){//arr is sorted
struct {
int value;
int occurs;
} tmp[n];
//make distinct array
int k = 0;
tmp[k].value = arr[0];
tmp[k].occurs = 1;
for(int i = 1; i < n; ++i){
if(arr[i] != arr[i-1]){
tmp[++k].value = arr[i];
tmp[k].occurs = 1;
} else {
++tmp[k].occurs;
}
}
//Written back
for(int i = 0, j = 0; i < n; ++i){
while(tmp[j].occurs == 0){
j = (j == k) ? 0 : j + 1;
}
arr[i] = tmp[j].value;
--tmp[j].occurs;
j = (j == k) ? 0 : j + 1;
}
}
int main(void){
int arr[] ={1, 1, 1, 2, 2, 3, 3};
int n = sizeof(arr)/sizeof(*arr);
cnv(n, arr);
for(int i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
For example an array: {1, 2, 3}
How do I make a program that loops this array to get an output of {1, -1, 2, -2, 3, -3}?
I tried doing something like:
#include <stdio.h>
int main()
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i, j, k;
for(i = 0; i < n*2; i++)
{
for(j = 0; j < n; j++)
{
for(k = 0; k < 2; k++)
{
if((i+1) % 2 != 0)
{
result[i] = arr[j];
}
else if((i+1) % 2 == 0)
{
result[i] = -arr[j];
}
}
}
}
for(i = 0; i < n*2; i++)
{
printf("%d ", result[i]);
}
}
But it only outputs {3, -3, 3, -3, 3, -3}
I thought my logic was perfect xD . Can anyone help?
Simple implementation: just store positive and negative values.
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int result[6];
int i;
for (i = 0; i < n; i++)
{
result[i * 2] = arr[i];
result[i * 2 + 1] = -arr[i];
}
for(i = 0; i < n * 2; i++)
{
printf("%d\n", result[i]);
}
return 0;
}
This block seems to work:
int src[] = { 1, 2, 3};
int dest[(sizeof(src) / sizeof(int)) * 2];
int destCount = 0;
for (int i = 0; i < sizeof(src) / sizeof(int); i++) {
dest[destCount++] = abs(src[i]);
dest[destCount++] = -(abs(src[i]));
}
for (int i = 0; i < (sizeof(src) / sizeof(int)) * 2; i++) {
printf("%d\n", dest[i]);
}
I put in abs() calls to make sure you get a positive number followed by a negative number. If this isn't what you want, then just get rid of them.
Or if you just want output you could do this:
#include <stdio.h>
int main(void)
{
int n = 3;
int arr[3] = {1, 2, 3};
int i;
for (i = 0; i < n; i++)
{
printf("%d ",arr[i]);
printf("%d ",-1*arr[i]);
}
return 0;
}