Find how many positive numbers there are in my array - c

How to find how many positive numbers there are in my code? I am getting the wrong output, please explain in detail where my mistake here is. I wish to have the exact output as desired. If I entered 6 as n then I will insert six numbers and the output will show me how many positive numbers I have inserted.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n;
scanf("%d",&n);
int arr[n];
int i;
int p = 0;
int arr_i;
for (arr_i = 0; arr_i < n; arr_i++) {
scanf("%d",&arr[arr_i]);
}
for (i = 0; i < n; i++) {
int arr_index=i;
if (arr[arr_index] > 0) {
p++;
}
printf("%d",p);
}
return 0;
}

Try this:
Just Print p outside the for loop.
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main() {
int n;
scanf("%d",&n);
int arr[n];
int i;
int p = 0;
int arr_i;
for (arr_i = 0; arr_i < n; arr_i++)
{
scanf("%d",&arr[arr_i]);
}
for (i = 0; i < n; i++)
{
int arr_index=i;
if (arr[arr_index] > 0)
{
p++;
}
}
printf("%d",p);
return 0;
}

YOu need t allocate dynamic memory using malloc - hope this helps
#include <stdio.h>
#include <stdlib.h>
int main() {
char c;
int n;
scanf("%d", &n);
int * arr;
int i;
int p = 0;
arr = (int *)malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
if (arr[i] > 0)
{
p++;
}
}
printf(" %d", p);
return 0;
}

Related

To generate 10 random numbers between -1.5 and 1.5 and write them in a ran.dat file

I have tried this
Fix the range and also compute average and number of data greater than average
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int i;
float num;
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat", "w");
for (i = 0; i < 9; i++)
{
num = rand() % (-1.5,1.5);
fprintf(fp, "%f\n", num);
}
fclose(fp);
return 0;
}
You can generalize it by creating a function like so:
float random_float(float min, float max)
{
return ((float)rand() / ((float)RAND_MAX)) * (max - min) + min;
}
With both min and max inclusive.
This is the complete code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float random_float(float min, float max)
{
return ((float)rand() / ((float)RAND_MAX)) * (max - min) + min;
}
int main()
{
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat", "w");
int i;
for (i = 0; i < 9; i++)
{
fprintf(fp, "%f\n", random_float(-1.5, 1.5));
}
fclose(fp);
return 0;
}
A few things I changed:
Declared the variable i immediately before the for loop. This is a good practice, declaring things only when you are about to use them;
Removed declaration of float num as it was unnecessary.
This will create 10 random numbers between -1.5 and 1.5.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(){
int i;
float num;
srand(time(NULL));
FILE *fp;
fp = fopen("ran.dat","w");
for(i=0; i < 10; i++)
{
num = (double)rand()/RAND_MAX * 3.0 - 1.5;
fprintf(fp,"%f\n", num);
}
fclose(fp);
return 0;
}

C for condition tricks

I found a challenge on the internet and I'm really stuck.
The goal is to print 20 times _ by adding/changing only 1 character (only one operation performed in total):
#include <stdio.h>
int main(void)
{
int i;
int n=20;
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
I have already found 1 solution but I can't find the last one? Is there some tricks I need to know about for loops ?
Replace i by n
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i < n; n--)
printf("*");
getchar();
return 0;
}
Put - before i
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; -i < n; i--)
printf("*");
getchar();
return 0;
}
Replace < by +
#include <stdio.h>
int main()
{
int i, n = 20;
for (i = 0; i + n; i--)
printf("*");
getchar();
return 0;
}
Source: https://www.geeksforgeeks.org/changeadd-only-one-character-and-print-exactly-20-times/
to correct the posted code to only output 20 times, you could use:
#include <stdio.h>
int main(void)
{
int i;
int n=-20; // note the minus 20
for(i=0;i<n;i--)
{
printf("_");
}
return 0;
}
If it is allowed you could write:
n=10; for(i=0;i<n;i++){printf("__");}
or
n=10; for(i=0;i<n;i++){printf("_");printf("_");}

Basic C help using arrays and for loops

#include "stdafx.h"
#include "stdio.h"
#include <string.h>
void main() {
int Results[8];
int i = 0;
int max = 0;
int maxindex;
printf("Enter the results of your 7 leavin cert subjects: ");
do {
printf("\nSubject %d: ", i + 1);
scanf_s("%d", Results);
i++;
} while (i < 7);
for (i < 7; Results[i] > 0; i++)
if (Results[i] > max)
max = Results[i];
printf("The best grade is %d", max);
}
Hello, so basically I'm trying to print out the largest number(Best result) by using a for loop. However it keeps telling me the that the best result is 0.
Does anybody know what I'm doing wrong. Any help would be greatly appreciated.
There are 2 major problems in your code:
You read all numbers into Results[0] with the scanf_s("%d", Results);. You should instead write:
if (scanf_s("%d", &Results[i]) != 1) {
/* not a number, handle the error */
}
The second loop is incorrect: for (i < 7; Results[i] > 0; i++) has multiple issues. Write instead for (i = 0; i < 7; i++)
And smaller ones too:
#include "stdio.h" should be written #include <stdio.h>
#include "stdafx.h" is not used, and so can be removed - regardless, it should be written as #include <stdafx.h> if it were to be used.
The Results array has size 8, but you only use 7 slots.
main should have prototype int main(void) or int main(int argc, char *argv[]) or equivalent.
favor idiomatic for (i = 0; i < 7; i++) loops over error prone do / while loops.
use braces for a non trivial loop body.
Here is a simpler and better version:
#include <stdio.h>
#include <string.h>
int main(void) {
int Results[7];
int i, n, max;
printf("Enter the results of your 7 leavin cert subjects: ");
for (i = 0; i < 7; i++) {
printf("\nSubject %d: ", i + 1);
if (scanf_s("%d", &Results[i]) != 1) {
printf("invalid number\n");
exit(1);
}
}
for (n = i, max = 0, i = 0; i < n; i++) {
if (Results[i] > max)
max = Results[i];
}
printf("The best grade is %d\n", max);
return 0;
}

reversing elements of an integer array

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20],int n);
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20],int n)
{
for(int i=n-1;i>=0;i--)
{
printf("%d ",a[i]);
}
return 0;
}
here if I input n=4 then during runtime i have to take 5 elements and then it reverses.For eg if i take n=4 and then for no of elements i have to take 1,2,3,4,5 and then only output is coming as 4 3 2 1.Why? is my logic wrong? also in this code I am unable to take the number of elements of arrays in a straight line, like 1 2 3 4.When I am entering the number each number is entering in new line .I am a novice programmer in C and thus having these doubts.Please anyone explain...
The problem with your code is the extra space after %d in your scanf line where you accept array elements i.e.
for(int i=0;i<n;i++)
{
scanf("%d ",&a[i]); //should be scanf("%d",&a[i]);
}
Change that and you're good to go.
Here is your entire program refactored to work correctly:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int reverse(int a[20], int n)
int main()
{
int a[20];
int n;
printf("enter the number of elements \n");
scanf("%d",&n);
printf("enter the array elements\n");
for (int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
reverse(a,n);
return 0;
}
int reverse(int a[20], int n)
{
int mid = n/2;
for (int i=0; i < mid; ++i)
{
int temp = a[n-i-1];
a[n-i-1] = a[i];
a[i] = temp;
}
return 0;
}

for() and random() FUNCTIONS

Why is this code not working properly?
The intent of this code is to generate and print two random numbers separately
as many times as user choose.
M.T.
Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) rand() % x
#define randomize srand((unsigned)time(NULL))
int i=0;
int j=0;
int x=0;
int y=0;
int main(void)
{
printf("insert number of loops:");
scanf("%d",x);
for(y=0;y=x;y++)
{
randomize;
i = random(51);
j = random(51);
printf("%d\n",i);
printf("%d\n",j);
}
return 0;
}
You are calling srand more than once. Place randomize; outside the for loop. Another problem is with the statement
scanf("%d",x);
you forget to place & before x.
Also You need to correct the loop condition y = x to y < x as mentioned in comment by #Cool Guy.
solved, thanks.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define random(x) rand() % x
#define randomize srand((unsigned)time(NULL))
int i=0;
int j=0;
int x=0;
int y=0;
int main(void)
{
printf("insert number of loops:");
scanf("%d",&x);
randomize;
for(y=0;y<x;y++)
{
i = random(51);
j = random(51);
printf("%d\n",i);
printf("%d\n",j);
}
return 0;
}

Resources