why output is 0 here. i am a newbie (20 days experience) - arrays

*i want to copy inputed array in sum_of_elements function as argument and then sum all the elements of array, but i am getting output 0.
#include <stdio.h>
int i, num, sum;
int sum_of_elements(int arr[]) {
for (i = 0; i < num; i++) {
for (i = 0; sum = 0, i < num; i++) {
sum += arr[i];
}
return sum;
}
}
int main() {
printf("enter number of digits you want to add\n");
scanf("%d", & num);
int arr[num];
for (i = 0; i < num; i++) {
printf("enter number %d\n", i + 1);
scanf("%d", & arr[i]);
}
int total = sum_of_elements(arr);
printf("%d", total);
return 0;

Look at this line of code:
for (i = 0; sum = 0, i < num; i++) {
This resets sum to 0 every loop.
It should be
for (i = 0, sum = 0; i < num; i++) {
But it's probably better to do this:
sum = 0;
for (i = 0; i < num; i++) {

The issue was with the double for loop in your sum_of_elements function.
Removing the extra for loop, resolves the error.
#include <stdio.h>
int i, num, sum;
int sum_of_elements(int arr[]) {
for (i = 0; i < num; i++) {
sum += arr[i];
}
return sum;
}
int main() {
printf("enter number of digits you want to add\n");
scanf("%d", & num);
int arr[num];
for (i = 0; i < num; i++) {
printf("enter number %d\n", i + 1);
scanf("%d", &arr[i]);
}
int total = sum_of_elements(arr);
printf("%d", total);
return 0;
}

Related

How can I move all elements in an array with a K number and make the numbers rotate when it comes to the end?

#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < N; i++)
{
int temp = arr[i];
arr[i + K] = arr[i];
arr[i] = temp;
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}
I know the current code is totally wrong It was just another test.
Basically what I need to do is something like this:
the array before: arr[N]={1,2,3,4,5,6,7,8,9,10}
the array after if K is 2: arr[N]={10,9,1,2,3,4,5,6,7,8}
Like #whozcraig pointed out for in-place rotation of array members.
Define a function to reverse(in-place) array members in a range :
static inline void
arr_reverse (const int start, const int end, int arr[]) {
for (int ai = start, zi = end; ai < zi; ++ai, --zi) {
int tmp = arr[ai];
arr[ai] = arr[zi];
arr[zi] = tmp;
}
}
Then you call it like :
K %= N;
if (K != 0) {
arr_reverse (0, N-1, arr);
arr_reverse (0, K-1, arr);
arr_reverse (K, N-1, arr);
}
I write something like this but it creates new array instead of modifying current one but it works.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int N, K, i;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Please enter value of K: ");
scanf("%d", &K);
int arr[N], newArr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
newArr[i] = arr[N-1-i];
}
for (int x = 0; i < N; i++)
{
newArr[i] = arr[x];
x++;
}
for (i = 0; i < N; i++)
{
printf("%d ", newArr[i]);
}
return 0;
}
Thank you guys, for all the comments and answers. I've tried them and they worked.
I have found a way to do it as well. It's bit different. I did it with a function which moves all the elements with 1 position and then repeat the func as much as needed (K times).
#Cheatah helped me come up with it. I will post it in case somebody likes this solution in the future.
Here it is:
#include <stdio.h>
#include <stdlib.h>
int moveOnePos(int num, int array[num])
{
int temp = array[num - 1];
for (int b = num - 1; b > 0; b--)
{
array[b] = array[b - 1];
}
array[0] = temp;
return 0;
}
int main()
{
int N, K, i;
printf("Please enter size of array: ");
scanf("%d", &N);
printf("Please enter K: ");
scanf("%d", &K);
int arr[N];
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < K; i++)
{
moveOnePos(N, arr);
}
for (i = 0; i < N; i++)
{
printf("%d", arr[i]);
}
return 0;
}

Used function to find the sum of the array elements, I am getting garbage value

I wrote a code for finding the sum of array elements using functions. I have written like this(mentioned below). I am getting a garbage value as output.
#include<stdio.h>
int fact(int n, int i) {
int sum = 0, arr[100];
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = fact(n, i);
printf("%d", sum);
return 0;
}
Pass the array arr to your function instead of re-declaring it inside fact
PS: Change the name of your function to something meaningful like arrSum.
Code:
#include<stdio.h>
int arrSum (int arr[], int n) {
int i, sum = 0;
for (i = 0; i < n; i++) {
sum = sum + arr[i];
}
return sum;
}
int main() {
int n, arr[100], i;
scanf("%d", &n);
if(n < 0 || n > 100)
return -1;
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int sum = arrSum(arr, n);
printf("%d ", sum);
return 0;
}

Adding a second loop

I want to add a second loop to my program, in which I let the user enter an number x in each loop and form the sum over the entered numbers. the result of this code is 378 but must be for example 378+x=380
int main()
{
int i;
int sum = 0;
for (i = 1; i <= 27; i++) {
sum += i;
}
printf("%d\n", sum);
return 0;
}
You write a loop inside your loop and add a scanf.
int i, j;
int sum = 0;
for (int i = 0; i <= 27; i++) {
for (j = 0; j < 10; j++) {
int x= 0;
scanf ("%d", &x);
sum += x;
}
}
If you add a second loop you will need to input y*x inputs where y is the frequency of 2nd loop.
The code you may be looking for is
int main () {
int i,x;
int sum = 0;
for(i = 1; i <= 27; i++)
{
scanf("%d",&x);
sum += i+x;
}
printf("%d\n", sum);
return 0;
}

Is there any method to print the value that user input with array?

It prints the wrong number/random number. What I want is to print numbers higher than 75.
int main() {
int array[5];
int num, i;
for (i = 1; i <= 5; i++) {
printf("Input Num %d : ", i);
scanf("%d", &num);
}
if (num >= 75) {
printf("%d\n", array[i]);
}
return 0;
}
Please use if within "for" loop. and please change "array" to "arr" or another name. The array will be a keyword in c++ sometime. should not use "array" to name a variable. here is my solution:
int main() {
int arr[5];
int num, i;
for (i = 1; i <= 5; i++) {
printf("Input Num %d : ", i);
num = 0;
scanf("%d", &num);
arr[i-1] = num;
}
for (i = 1; i <= 5; i++) {
if (arr[i - 1] >= 75) {
printf("%d\n", arr[i - 1]);
}
}
return 0;
}
You have a couple of errors:
You iterate over the wrong set. Arrays in C start from 0, NOT 1.
You never set any value for array. What should it contain then? Random stuff of course. You could avoid it by initializing your array to zero. That way you know you are not writing to your array if you get zeros in your output.
Code:
#include <stdio.h>
int main() {
int array[5] = {};
int num = 0,i;
for ( i = 0; i <5; i++) {
printf("Input Num %d : ",i );
scanf("%d",&num );
array[i] = num;
}
for ( i = 0; i <5; i++) {
if (array[i] >= 75) {
printf("%d\n",array[i]);
}
}
return 0;
}

HP Code Wars Check digit algorithm error

QUESTION:
http://www.hpcodewars.org/past/cw17/problems/Prob02--CheckDigit.pdf
Here's my code:
int checkdigit(){
int n,i,j,sum1,sum2,k;
char ch;
printf("Enter the number of lines.Then enter ther the codes!");
scanf("%d",&n);
char *codes[n];
int msum[n];
int fsum[n];
for(i=0;i<n;++i){
scanf("%s",codes[i]);
}
for(i=0;i<n;++i){
for(j=0,k=3;j<21;j+=3,k+=3){
char *num;
num=codes[i];
ch=num[j];
sum1+=atoi(ch);
if(k<21)
ch=num[k];
sum2+=atoi(ch);
}
msum[i]=sum1*3;
fsum[i]=((msum[i]+sum2)%10);
if(fsum[i]!=0)
fsum[i]-=10;
}
for(k=0;k<sizeof(fsum);k++){
printf("%s %d",codes[k],fsum[k]);
}
return 0;
}
The Code now crashes after taking the first UPC code as input.
Change
fsum[i]=((msum+sum2)%10);
to
fsum[i]=((msum[i]+sum2)%10);
That is because msum is an array of integers, and msum[i] is an integer. As msum is an array, it is of type int* and is not compatible with int for the binary operator %
Here
char *codes[n];
is array of n pointers and you don't allocate memory for these pointers and try to scan values to this location so you see a crash
#include <stdio.h>
int checkdigit(int data[12]){
int i, even, odd, result;
even = odd = 0;
for(i = 0; i < 11; ++i){
if(i & 1)
even += data[i];
else
odd += data[i];
}
result = (odd * 3 + even) % 10;
if(result)
result = 10 - result;
return data[11] = result;
}
int main(){
int n;
scanf("%d", &n);
int codes[n][12];
int i, j;
for(i = 0; i < n; ++i){
for(j = 0; j < 11; ++j){
scanf("%d", &codes[i][j]);
}
checkdigit(codes[i]);
}
for(i = 0; i < n; ++i){
for(j = 0; j < 12; ++j){
if(j) putchar(' ');
printf("%d", codes[i][j]);
}
putchar('\n');
}
return 0;
}

Resources