read from file and put to a dynamic array and prime number

i need your help to these problems please

1. write a program which puts 1000 random numbers to a file data.in, each number will be >=0 and <=200. At the first line of the file you have to write the large number of the numbers.
Then create another which loads the numbers of the file data.in to a dynamic array.

the first program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 1000

int main()
{


int num,i;

FILE *f;
srand(time(NULL));

f=fopen("C:/data.in","w");
fprintf(f,"%d\n",N);

for(i=0;i<N;i++){
num=rand()%(200+1);
fprintf(f,"%d\n",num);
}


fclose(f);
system("pause");
return 0;
}






the second program (i'm really stuck on this, i messed it up)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<stdlib.h>
#define N 1000
int main()
{
	int num,i;
	int p[N];

	FILE *f;

	f=fopen("C:/data.in","r");
	

	a=malloc (int *)(N(int));
	
	for (i=0; i < N; i++)
 	{
    		fscanf(f, "%d", &num);    
		printf("%d\n",p[i]); 
}






2. write a program which shows when a number is prime

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;

class PrArithmoi
{


static void Main()


{
bool flag= true;
int N,i;
Console.WriteLine("give a number");
N = Int32.Parse(Console.ReadLine());

if (N != 1)
flag= false;

for (i = 2; i < N - 1; i++)
{
if (N % i == 0)
flag = false;
}
if (!flag)
Console.WriteLine("prime number");
else
Console.WriteLine("not prime number");
Console.ReadKey();




}


} 


this doesn't work well. for instance the number 4 isn't prime but it prints me prime...
Wotcher!

About the second program: The easiest (albeit not the most efficient) way to do this (off the top of my head) is to use realloc and a loop. Each time you read another line, realloc to expand your array by one and add in the new value. I'm assuming you're using C for a reason; if you can use C++ I'd suggest a vector instead of repeated reallocs. Also, note how you're supposed to use realloc: first, you need a pointer to a previously alloced bit of memory, second you need the size of the new block in bytes (so sizeof(int)*elements).

About the third program: Are you sure you meant to put what you put at line 22? Also, maybe jumping out of the loop after finding a number that divides perfectly by the prime might be a good idea.

Happy coding!

-Albatross
Last edited on
these are exercises for my school. Our teacher didn't teach us realloc....
he showed us once malloc but it confuses me....

about the block in bytes you said, i have to change my code like this?

malloc(sizeof(int)*N);


about the third program i'm not sure about the line 22...
I kind-of figured that these are homework problems, but I would be a bit surprised if your teacher deduced points for using something he didn't teach you about. Although then again...

And... yes.

And... well, about line 22. I can't compile C# programs because I don't have a C# compiler, but reading through it makes me wonder whether flag could be true for any value of N other than 1. If you enter any value of N other than 1, flag will be set to false (prime number), and line 22 won't change that when I think it should.

I need to sleep more...

-Albatross
about our teacher

each time he shows us 3 or 4 exercises and gives us 3-4 exercises for homework with things he doesn't shows us in the classroom....
besides this his programs have errors..... so, this forum is a gift from God!


yeah the third program does exactly what you said, all numbers except 1 prints me as primes....
You might also want to consider changing the name for your flag, which would make it a lot easier to remember what it's supposed to mean. Right now, it's supposed to mean something along the lines of "N is not prime".

Given that, look at lines 16, 17, 21, and 22. Are you doing anything that contradicts that definition?

Notes:
-1 is, for all practical purposes, not prime.
-If you find a number that's not equal to 1 or the number that divides the number perfectly, it's probably not prime.

Good luck!

-Albatross
ok, i will try it more....

someone for the other two please?
Topic archived. No new replies allowed.