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.
#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
using System;
class PrArithmoi
{
staticvoid 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...
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.
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.
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.