I am a beginner in c and i was writing this piece of code. This is the first time im using doubles so it might be related.
The code gives the print statement in the main function, then when it enters my function ReadVector() it stops working.
I want to to learn and fix my mistake, any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%f",*(x + i));
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
Try this one:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void ReadVector(double *x,int size){
printf("Enter the values of vector: \n");
for(int i=0;i<size;i++)
scanf("%lf", &x[i]);
}
int main(){
int m;
printf("Enter the size of vector: ");
scanf("%d",&m);
double *arr= (double*)malloc(m*sizeof(double));
ReadVector(arr,m);
}
Related
#include <stdio.h>
#include <stdlib.h>
#include "simpio.h"
#include "stdio.h"
int main()
{
float answer;
int D;
int N;
int i=0;
int p=1;
printf("How much :");
N=GetInteger();
for (i=0; i!=N; i++)
{
for(p=1; p!=N; p++){
D=1/p;
answer+=D;
}
}
printf("the answer is: %.2f",apotelesma);
return 0;
}
for example if I gave N=100 then the program was suppose to
1/1+1/2+1/3....1/N
and then give me the 5.19
but for some reason its just skips it
I know it's an easy question I started programing like for two weeks ma trying to learn alone.
Replace the inner-loop with this to force floating-point math:
for(p=1; p!=N; p++)
answer += 1.0/p;
Also initialise float answer = 0;.
I using a old version of Borland for C lang.
At the beginning of the program you enter the name (full name, FIO), then 4 digits (as grades). The program calculates the average among 5 entered FIO and back a average number.
#include <stdio.h>
#include <conio.h>
int main(){
struct nya{
char a[100];
int x[4];
}A[5];
int i;
for(i=0;i<5;i++){
puts("FIO");
scanf("%s", A[i].a);
puts("4 ocenki");
for(int g=0;g<4;g++){
scanf("%i", A[i].x[g]);
}
clrscr();
}
float bird=0, comme[5];
for(i=0;i<5;i++){
comme[i]=0;
}
for(i=0;i<5;i++){
for(int g=0;g<4;g++){
comme[i]+=A[i].x[g];
}
comme[i]=comme[i]/4;
bird+=comme[i];
}
bird=bird/5;
printf("Sredny = %f", bird);
}
change scanf("%i", A[i].x[g]); to scanf("%i", &A[i].x[g]); (with a &)
thanks to #Blaze
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}
Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}
#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}
I'm really new to C and I can't get this while loop to work. It just exits the loop for no apparent reason.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int min;
int max;
int random;
int guess;
char user='x';
printf("Please insert min number:");
scanf("%d",&min);
printf("Please insert max number:");
scanf("%d",&max);
random = (rand()%(max-min))+ min;
do
{
printf("please insert your guess:");
scanf("%d",&guess);
if(guess==random)
{
printf("YOU WON");
user=='y';
}
else if(guess!=random)
{
printf("UNLUCKY,would you like to play again:x for yes");
user=='x';
}
}while(user=='x');
}
The following is a comparison, not an assignment:
user=='y';
You need to change it to:
user='y';
(Note the single =.)
The same goes for the other assignment.