can you help me with code which returns partial sum of 'X' numbers in array in c?
Complete :
int arr_sum( int arr[], int n )//Recursive sum of array
{
if (n < 0) //base case:
{
return arr[0];
}
else
{
return arr[n] + arr_sum(arr, n-1);
}
}
void sum_till_last (int *ar,int si )
{
**int sum,i;// the problem some where here
ar=(int*) malloc(si*sizeof(int));
for (i=0; i<si;i++)
{
sum=arr_sum(ar,i);
ar [i]=sum;
}
free (ar);**
}
void main ()
{
int i;
int a [5];
for (i = 0; i < 5; i++)
scanf_s("%d", &a[i]);
sum_till_last(a,5);
printf("%d\n",a[5]);
}
\i want to create new array with this this legality:
My input :
4
13
23
21
11
The output should be (without brackets or commas):
4
17
40
61
72
Now when we can see the full code, it's quite obvious that the problem is in the sum_till_last function where you overwrite the pointer you pass to the function with some new and uninitialized memory you allocate.
Drop the allocation (and the call to free of course). And fix the logical bug in arr_sum that causes you to get arr[0] + arr[0] when i is zero.
Here you go:
#include <stdio.h>
int main () {
int in_arr[5] = {4,13,23,21,11};
int out_arr[5];
int p_sum =0;
int i;
for ( i=0;i<5;i++){
out_arr[i] = in_arr[i]+p_sum;
p_sum=p_sum+in_arr[i];
}
for (i=0;i<5;i++){
printf("%d", out_arr[i] );
}
}
Fix according to your policy
#include <stdio.h>
#include <stdlib.h>
int arr_sum(int arr[], int n){
if (n == 0){//Change to this
return arr[0];
} else {
return arr[n] + arr_sum(arr, n-1);
}
}
void sum_till_last(int *ar, int si){
int sum,i;
int *temp = malloc(si * sizeof(int));//variable name ar is shadowing parameter name ar.
for(i = 0; i < si; i++){
temp[i] = arr_sum(ar, i);
if(i)
putchar(' ');
printf("%d", temp[i]);//need print out :D
}
free(temp);
}
int main(void){
int i, a[5];
for (i = 0; i < 5; i++)
scanf_s("%d", &a[i]);
sum_till_last(a, 5);
//printf("%d\n",a[5]);<-- this print only one element. and It's out of bounds element XD
}
I just made it simple so it´s easy to understand :)
I´m assuming "n" is always equal or less then array element number. Then you just print the SUM.
#include <stdio.h>
int arr_sum( int arr[], int n ){
int i=0,SUM=0;
for(; i < n;i++){
SUM= SUM+ arr[i];
printf("%d ",SUM);
}
}
int main(void) {
int array[] = {4, 13, 23, 21, 11};
arr_sum(array,5);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
void Print_Array(int values[], int length);
void swap(int values[], int i, int j);
void Move_Max(int values[], int max_index);
void Simple_Sort(int values[], int length);
int main() {
How do I use these numbers?
Is SIZE & length one and the same thing?
int my_vals[SIZE] = {83, 89, 94, 73, 11, 33, 25, 34, 73, 41};
Print_Array(my_vals, SIZE); //<- FIRST CALL TO PRINT
Simple_Sort(my_vals, SIZE);
system("PAUSE");
}
void Simple_Sort(int values[], int length) {
int i;
So here length would be 10?
I am starting from the end of my_vals? So would I be starting at 73? Since it's 10-1 = 9? So the 9th would be 73?
for (i = length - 1; i > 0; i--)
{
Move_Max(values, i);
Print_Array(values, SIZE);
}
}
void Move_Max(int values[], int max_index) {
int max, i, maxi;
max = values[0];
maxi = 0;
for (i = 1; i <= max_index; i++)
{
if (max < values[i])
{
max = values[i];
maxi = i;
}
}
swap(values, maxi, max_index);
}
void swap(int values[], int i, int j) {
int temp;
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
void Print_Array(int values[], int length) {
int i;
for ( i = 0; i < length; i++)
printf("%d", values[i]);
printf("\n");
}
When you declare an array, say you have declared int array A[4] so it means that an array A has length of 4 i.e. A[0] to A[3].
In your case, my_vals array is of 10 length i.e. my_vals[0] to my_vals[9]
my_vals[0] = 83
..
..
my_vals[9] = 41
In your for loop you are iterating from last i.e. from my_vals[9] i.e. 41
for (i = length - 1; i > 0; i--)
So the initial value of i will be 9.
But here you need to iterate till i = 0 i.e.
for (i = length - 1; i >= 0; i--)
In my code the following function exists:
int Count_border(int loc[], int search[], int search_c){
int count = 0, i, j;
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
}
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
}
return count;
}
In this function I am searching for values in the array search. How it is done doesn't matter for this question. My question is however, how can I input a "manual" array, like this: Count_border(con_input, {-1, 0, 1}, 3);
This syntaxis isn't allowed by the compiler. And I don't want to create an array before the function, I really want to hardcode it.
Thank you for your help!
EDIT:
Now I am getting this error:
In function 'main':
file.c:40:1: error: expected ';' before '}' token
}
^
file.c:85:1: error: expected declaration or statement at end of input
}
Where this is my whole code, PLEASE help me out.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void Put(int letter, int number);
void Set(int letter, int number, int who);
void Init_board();
int Count_border(int loc[], int search[], int search_c);
int In_array(int val, int arr[], int size);
int BOARD[9][9]; // 0 = empty, 1 = me, 2 = enemy
int STONES[2][81][9][9], STONE_COUNTER[2];
int main(void){
char input[5];
int con_input[2], t;
Init_board();
memset(STONES, 0, sizeof(STONES));
memset(STONE_COUNTER, 0, sizeof(STONE_COUNTER));
scanf("%s", input);
if(strcmp(input,"Start") == 0){
Put(4, 4);
}
scanf("%s", input); //get the first input after start
do{
con_input[0] = input[0]-'a'; /* Convert the input */
con_input[1] = input[1];
Set(con_input[0], con_input[1], 2);
t = Count_border(con_input, (int[]){-1, 0, 1}, 3);
printf("%i\n", t);
scanf("%s", input); /* Get the next input */
} while(strcmp(input, "Quit") != 0)
}
void Init_board(){
int i,j;
memset(BOARD, -1, sizeof(BOARD));
for(i = 0; i < 9; i++){
for(j = 0; j < 9; j++){
BOARD[i][j] = 0;
}
}
}
void Put(int letter, int number){
char t = letter + 'a';
printf("%c%i\n", t, number);
//fflush(stdout);
Set(letter, number, 1);
}
void Set(int letter, int number, int who){
BOARD[letter][number] = who;
}
int Count_border(int loc[], int search[], int search_c){
int count = 0, i, j;
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0] + j][loc[1]], search, search_c) == 1) count++;
}
for(j = -1; j < 2; j += 2){
if(In_array(BOARD[loc[0]][loc[1] + j], search, search_c) == 1) count++;
}
return count;
}
int In_array(int val, int arr[], int size){
int i;
for (i=0; i < size; i++) {
if (arr[i] == val)
return 1;
}
return 0;
}
/* notes:
fflush(stdout);
*/
If you have a C99 (or newer) compiler just do
Count_border(con_input, (int[]){-1, 0, 1}, 3);
this (int[]){ something } is called a compound literal in C jargon and defines a temporary object (here an int array with 3 elements) that you can pass to your function.
Something like this?
#include <stdio.h>
void f(char arr[]);
int main(int argc, char *argv[])
{
f((char [4]){'1', '2', '3', '5'});
return 0;
}
void f(char arr[4])
{
int i;
for (i = 0; i < sizeof(arr)/sizeof(*arr); i++)
printf("%c ", arr[i]);
putchar('\n');
}
I'm working on a small program that counts the number of times an integer appears in an array.
I managed to do this but there is one thing I can't overcome.
My code is:
#include <stdio.h>
int count_occur(int a[], int num_elements, int value);
void print_array(int a[], int num_elements);
void main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i<20; i++)
{
num_occ = count_occur(a, 20, a[i]);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i<num_elements; i++)
{
if (a[i] == value)
{
++count; /* it was found */
}
}
return(count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
My output is :
Array:
2 5 0 5 5 66 3 78 -4 -56 2 66 -4 -4 2 0 66 17 17 -4
The value 2 was found 3 times.
The value 5 was found 3 times.
The value 0 was found 2 times.
The value 5 was found 3 times.
The value 5 was found 3 times.
The value 66 was found 3 times.
The value 3 was found 1 times.
The value 78 was found 1 times.
The value -4 was found 4 times.
The value -56 was found 1 times.
The value 2 was found 3 times.
The value 66 was found 3 times.
The value -4 was found 4 times.
The value -4 was found 4 times.
The value 2 was found 3 times.
The value 0 was found 2 times.
The value 66 was found 3 times.
The value 17 was found 2 times.
The value 17 was found 2 times.
The value -4 was found 4 times.
How can I avoid double lines in the output?
You can use a parallel array, this example uses char[20] in order to save some space:
#include <stdio.h>
int count_occur(int a[], char exists[], int num_elements, int value);
void print_array(int a[], int num_elements);
int main(void) /* int main(void), please */
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char exists[20] = {0}; /* initialize all elements to 0 */
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i < 20; i++)
{
num_occ = count_occur(a, exists, 20, a[i]);
if (num_occ) {
exists[i] = 1; /* first time, set to 1 */
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}
int count_occur(int a[], char exists[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i < num_elements; i++)
{
if (a[i] == value)
{
if (exists[i] != 0) return 0;
++count; /* it was found */
}
}
return (count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i<num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
This method is faster, as it skips values already readed and starts iterating from i in count_ocurr:
#include <stdio.h>
int count_occur(int a[], char map[], int num_elements, int start);
void print_array(int a[], int num_elements);
int main(void)
{
int a[20] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
char map[20] = {0};
int num_occ, i;
printf("\nArray:\n");
print_array(a, 20);
for (i = 0; i < 20; i++)
{
if (map[i] == 0) {
num_occ = count_occur(a, map, 20, i);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
}
int count_occur(int a[], char map[], int num_elements, int start)
/* checks array a for number of occurrances of value */
{
int i, count = 0, value = a[start];
for (i = start; i < num_elements; i++)
{
if (a[i] == value)
{
map[i] = 1;
++count; /* it was found */
}
}
return (count);
}
void print_array(int a[], int num_elements)
{
int i;
for (i = 0; i< num_elements; i++)
{
printf("%d ", a[i]);
}
printf("\n");
}
I would suggest only printing the statement if the current index is also the index of the first occurrence of the number in question.
Inside count_occur, you have the index of each match in i. If you pass in the i from main to count_occur, you can do something such as returning -1 if that value is greater than the i in count_occur. Then if you get that -1 in main, don't print.
In addition, your algorithm could be made faster. Instead of searching the array linearly every time, you can sort a copy of the array so that the search can be done efficiently. (Even if you use one array to index and the other to search, it'll be faster - and still return values in the same order.)
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int count_occur(int a[], int num_elements, int value, bool selected[]);
void print_array(int a[], int num_elements);
int main(void){
int a[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int size = sizeof(a)/sizeof(*a);
bool ba[size];
memset(ba, 0, sizeof ba);
int num_occ, i;
printf("\nArray:\n");
print_array(a, size);
for (i = 0; i<size; i++){
if(ba[i] == true) continue;//skip already count
num_occ = count_occur(a, 20, a[i], ba);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
int count_occur(int a[], int num_elements, int value, bool ba[]){
int i, count = 0;
for (i = 0; i<num_elements; i++){
if (a[i] == value){
ba[i] = true;
++count;
}
}
return count;
}
void print_array(int a[], int num_elements){
int i;
for (i = 0; i<num_elements; i++){
printf("%d ", a[i]);
}
printf("\n");
}
Little improvement
int count_occur(int a[], int num_elements, int index, bool selected[]);
num_occ = count_occur(a, 20, i, ba);
int count_occur(int a[], int num_elements, int index, bool ba[]){
int i, count = 0;
for (i = index; i<num_elements; i++){
if (a[i] == a[index]){
ba[i] = true;
++count;
}
}
return count;
}
#include<stdio.h>
#include<string.h>
int main()
{
int arr[] = {2, 5, 0, 5, 5, 66, 3, 78, -4, -56, 2, 66, -4, -4, 2, 0, 66, 17, 17, -4};
int arrSize = sizeof(arr)/sizeof(arr[0]);
int tracker[20];
int i,j,k=0,l=0,count,exists=0;
for (i=0;i<arrSize;i++)
printf("%d\t", arr[i]);
printf("\n");
memset(tracker, '$', 20);
for (i=0, j=i+1, count=1, l=0; i<arrSize; i++)
{
j=i+1;
count=1;
l=0;
while (l < arrSize)
{
if (arr[i] == tracker[l])
{
exists = 1;
break;
}
l++;
}
if (1 == exists)
{
exists = 0;
continue;
}
while (j < arrSize)
{
if (arr[i] == arr[j])
count++;
j++;
}
tracker[k] = arr[i];
k++;
printf("count of element %d is %d\n", arr[i], count);
}
}
very simple logic to count how many time a digit apper
#include<stdio.h>
int main()
{
int a,b,c,k[10];
int p[10]={0};
int bb[10]={0};
scanf("%d\n",&a);
for(b=0;b<a;b++)
{
scanf("%d",&k[b]);
}
for(b=a-1;b>0;b--)
{
for(c=b-1;c>=0;c--)
{
if((k[b]==k[c])&&(bb[c]==0))
{
p[b]=p[b]+1;
bb[c]=1;
}
}
}
for(c=0;c<a;c++)
{
if(p[c]!=0)
{
printf("%d is coming %d times\n",k[c],p[c]+1);
}
}
return 0;
}
In your function :
int count_occur(int a[], int num_elements, int value)
/* checks array a for number of occurrances of value */
{
int i, count = 0;
for (i = 0; i<num_elements; i++)
{
if (a[i] == value)
{
++count; /* it was found */
a[i] = INFINITY; // you can typedef INFINITY with some big number out of your bound
}
}
return(count);
}
And in main() you can edit for loop:
for (i = 0; i<20; i++)
{
if(a[i] != INFINITY)
{
num_occ = count_occur(a, 20, a[i]);
printf("The value %d was found %d times.\n", a[i], num_occ);
}
}
It is possible to solve with two arrays
#include <stdio.h>
int main() {
int array[12] = {1,2,2,5,2,5,7,6,2,4,2,4};
int array2[100] = {0};
int indicator = 1;
int i = 0,j;
int index = 0;
int number_count;
for(int i = 0; i<12;i++) {
indicator = 1;
for(j = i+1;j<12;j++) {
if(array[i] == array[j]){
indicator = -1;
break;
}
}
if(indicator == 1){
array2[index] = array[i];
index++;
}
}
for(int k = 0; array2[k]; k++) {
number_count = 0;
for(int m = 0; m<12;m++){
if(array2[k] == array[m]){
number_count++;
}
}
printf("%d was found %d times...\n",array2[k],number_count);
}
return 0;
}
I am writing a program to add two large numbers in C.
My integer array result holds the sum of the two numbers (which were also stored in arrays).
For example, if the result array is [0,0,3,2] (actual array size is 20)
If 32 is my actual result, how can I display the contents of the result array without the leading zeros ?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BASE 10
void align(int A[],int n);
void add(int A[],int B[], int C[]);
void Invert(int* a, int n);
int main(int argc, char** argv){
char input1[20];
char input2[20];
int size = 20;
int a;
int b;
int num1[20];
int num2[20];
int result[20];
int length1 = strlen(argv[1]);
int length2 = strlen(argv[2]);
int i = 0;
for (i=0;i<length1;i++){
input1[i] = argv[1][i];
}
for (i=0;i<length2;i++){
input2[i] = argv[2][i];
}
a=atoi(input1);
b=atoi(input2);
align(num1,a);
align(num2,b);
add(num1,num2,result);
Invert(result,size);
for (i=0;i<20;i++){
printf("%d",result[i]);
}
return 0;
}
void align (int A[], int n){
int i = 0;
while (n) {
A[i++] = n % BASE;
n /= BASE;
}
while (i < 20) A[i++] = 0;
}
void add (int A[], int B[], int C[]) {
int i, carry, sum;
carry = 0;
for (i=0; i<20; i++) {
sum = A[i] + B[i] + carry;
if (sum >= BASE) {
carry = 1;
sum -= BASE;
} else
carry = 0;
C[i] = sum;
}
if (carry) printf ("overflow in addition!\n");
}
void Invert(int* a, int n)
{
int i;
int b;
for(i=0; i<n/2; i++){
b = a[i];
a[i] = a[n-i-1];
a[n-i-1] = b;
}
}
`
To get the actual digits (I assume that each digit is stored as a byte in an array of 20 bytes, lowest digit at highest index), you do something like this:
int i;
int size = sizeof(thearray) / sizeof(thearray[0]);
/* find first non-0 byte, starting at the highest "digit" */
for (i = 0; i < size - 1; ++i)
if (thearray[i] != 0)
break;
/* output every byte as character */
for (; i < size; i++)
printf("%c", thearray[i] + '0'); /* 0 --> '0', 1 --> '1', etc. */
printf("\n");
You can do this by below code:-
int flag=1;
for(i=0;i<20;i++)
{
if(flag==1&&array[i]!=0)
flag=0;
if(flag!=1)
{
printf("%d",array[i]);
}
}
This will remove all leading zeros.
I propose a solution by using the pointer. The situation where only zero is stored in the array is also handled. I'm more comfortable with the pointer.
int test[20] = {0,0,0,0,1,2,3,4,5,6,7,8,9,0,0,1,2,3,4,5};
int test_bis[20] = {0};
int * ptr_test = test_bis;
int ii = 0;
while( *(ptr_test)== 0 && ii < 20 ) {
ptr_test++;
ii++;
}
if( ii < 20)
do {
printf("%d",*(ptr_test));
ptr_test++;
} while (++ii < 20);
else
printf("0");
Thats for integer array you can modify it accordingly.
for(i=0;i<20;i++){
if(flag==1&&array[i]==0)
{
// just skips until first nonzero
}
else if(flag==1&&array[i]!=0){
flag=0; // when first nonzero comes set flag to 0 and print it
printf("%d",array[i]);
}
else {
printf("%d",array[i]); // after first nonzero simply print it
}
}