samedi 4 octobre 2014

Correction Exercice d'algorithme (Les tableaux à une seule dimension)

Corrigé :

1.
Module Module1
Dim i, n As Integer
Sub main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n), tcopy(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
For i = 0 To n - 1
tcopy(i) = t(n - 1 - i)
Console.Write(tcopy(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

2.
Module Module1
Dim n, i, x, pos, temp As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ") = ")
t(i) = Console.ReadLine
Next
n = n + 1
Console.Write("Entre l'élément à ajouter : ")
x = Console.ReadLine
Console.WriteLine(" ")
Console.Write("Entre sa position : ")
pos = Console.ReadLine
For i = pos To n - 1
temp = t(i)
t(i) = x
x = temp
Next
For i = 0 To n - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

3.
Module Module1 
Sub Main()
Dim i, n, x, j, temp As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ") = ")
t(i) = Console.ReadLine
Next
n += 1
Console.Write("Entrez le numero à ajouter : ")
x = Console.ReadLine
t(i) = x
For i = n - 1 To 1 Step -1
For j = 1 To i
If t(j - 1) > t(j) Then
temp = t(j)
t(j) = t(j - 1)
t(j - 1) = temp
End If
Next
Next
For i = 0 To n - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

4.
Module Module1
Sub Main()
Dim i, pos, n, x As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
Console.Write("Entrez l'élément à supprimé : ")
x = Console.ReadLine
For i = 0 To n – 1
If x = t(i) Then
pos = i
End If
Next
For i = pos To n - 1
t(i) = t(i + 1)
Next
For i = 0 To n - 2
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

5. 
Module Module1
Dim i, j, n As Integer
Sub Main()
Console.Write("n : ")
n = Console.ReadLine
Dim tcal(n + 1), taff(n + 1) As Integer
i = 1
tcal(0) = 1
tcal(1) = 1
Console.WriteLine(tcal(0) & " " & tcal(1))
i = i + 1
While (i <= n)
taff(0) = 1
For j = 1 To i
taff(j) = tcal(j) + tcal(j - 1)
Next
For j = 0 To i
Console.Write(taff(j) & " ")
Next
Console.WriteLine("")
For j = 1 To i
tcal(j) = taff(j)
Next
i = i + 1
End While
Console.ReadLine()
End Sub
End Module

6.
Module Module1
Dim n, c, i As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n), taff(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ") = ")
t(i) = Console.ReadLine
Next
c = 0
For i = 0 To n - 1
If t(i) Mod 2 = 0 Then
taff(c) = t(i)
c += 1
End If
Next
For i = 0 To n - 1
If t(i) Mod 2 <> 0 Then
taff(c) = t(i)
c += 1
End If
Next
For i = 0 To n - 1
Console.Write(taff(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

7.
Module Module1
Dim n, P, i As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim U(n), V(n) As Integer
For i = 0 To n - 1
Console.Write("U(" & i + 1 & ") = ")
U(i) = Console.ReadLine
Next
For i = 0 To n - 1
Console.Write("V(" & i + 1 & ") = ")
V(i) = Console.ReadLine
Next
For i = 0 To n - 1
P += U(i) * V(i)
Next
Console.WriteLine("Le produit scalaire : " & P)
Console.ReadLine()
End Sub
End Module

8.
Module Module1
Sub Main()
Dim i, n, pos, max, c As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ") = ")
t(i) = Console.ReadLine
Next
max = 0
c = 1
For i = 1 To n - 1
If t(i) = t(i - 1) Then
c = c + 1
Else
c = 1
End If
If c > max Then
max = c
pos = i - c + 2
End If
Next
Console.WriteLine("Le plus long plateau commence à l'indice : " &
pos)
Console.Write("Il est de longueur : " & max)
Console.ReadLine()
End Sub
End Module

9.
a.
Module Module1
Sub Main()
Dim i, n, j, temp As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
For i = n - 1 To 1 Step -1
For j = 1 To i
If t(j - 1) > t(j) Then
temp = t(j)
t(j) = t(j - 1)
t(j - 1) = temp
End If
Next
Next
For i = 0 To n - 1
Console.Write(t(i) & " ")
Next
Console.WriteLine(" ")
Console.ReadLine()
End Sub
End Module
b.
Module module1
Sub main()
Dim i, j, n, m, temp As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
For i = 0 To n - 2
m = i
For j = i + 1 To n - 1
If t(j) < t(m) Then
temp = t(m)
t(m) = t(j)
t(j) = temp
m = i
End If
Next
Next
For i = 0 To n - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module
c.
Module Module1
Dim i, j, n, elt As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
For i = 1 To n - 1
elt = t(i)
j = i
While t(j - 1) > elt And j > 0
t(j) = t(j - 1)
j = j - 1
If j = 0 Then
Exit While
End If
End While
t(j) = elt
Next
For i = 0 To n - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

10.
Module Module1
Dim i, V, n As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
V = t(n - 1)
Console.Write("La valeur du dernier entier de la séquence est : "
& V)
Console.ReadLine()
End Sub
End Module

11.
Module Module1
Sub Main()
Dim i, n, x, d, min As Integer
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n), td(x) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
x = -1
Console.Write("D = ")
d = Console.ReadLine
For i = 0 To n - 1
If t(i) > d Then
x = x + 1
ReDim Preserve td(x)
td(x) = t(i)
End If
Next
min = td(0)
For i = 0 To td.Length - 1
If td(i) < min Then
min = td(i)
End If
Next
Console.Write("La petite durée supérieur à D est : " & min)
Console.ReadLine()
End Sub
End Module

12.
Module Module1
Dim i, n As Integer
Dim Test As Boolean = True
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t(i) = Console.ReadLine
Next
For i = 0 To n - 1
If t(i) < 0 Then
Test = False
End If
Next
If Test = True Then
Console.WriteLine("La séquence comporte que des entiers
positifs.")
Else
Console.WriteLine("La séquence ne comporte pas que des
entiers positifs.")
End If
Console.ReadLine()
End Sub
End Module

13.
Module Module1
Dim i, n, m, Temp, j, c, s As Integer
Sub Main()
c = 0
Console.Write("Entrez la taille du Tableau 1 : ")
n = Console.ReadLine
Dim t1(n) As Integer
For i = 0 To n - 1
Console.Write("t1(" & i + 1 & ")=")
t1(i) = Console.ReadLine
Next
Console.Write("Entrez la taille du Tableau 2 : ")
m = Console.ReadLine
Dim t2(n) As Integer
For i = 0 To m - 1
Console.Write("t2(" & i + 1 & ")=")
t2(i) = Console.ReadLine
Next
s = m + n
Dim t(s) As Integer
For i = 0 To n - 1
t(i) = t1(i)
Next
For i = n To s
t(i) = t2(c)
c = c + 1
Next
For i = s - 1 To 1 Step -1
For j = 1 To i
If t(j - 1) > t(j) Then
Temp = t(j)
t(j) = t(j - 1)
t(j - 1) = Temp
End If
Next
Next
For i = 0 To s - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

14.
Module Module1
Dim i, j, c, n As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau : ")
n = Console.ReadLine
Dim t(n) As String
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ") = ")
t(i) = Console.ReadLine
Next
Dim ch As String
For i = 0 To n - 1
ch = t(i)
c = 0
For j = 0 To ch.Length - 1
If ch(j) = "a" Or ch(j) = "A" Then
c += 1
End If
Next
If c >= 3 Then
Console.WriteLine(t(i) & " ")
End If
Next
Console.ReadLine()
End Sub
End Module

15.
Module Module1
Dim i, c As Integer
Dim ch, s As String
Sub Main()
Console.Write("Entrez la chaîne : ")
ch = Console.ReadLine
c = 0
s = ch(0)
For i = 1 To ch.Length - 1
If ch(i) = " " Then
If ch(i + 1) <> s Then
c += 1
End If
End If
Next
If c = 0 Then
Console.Write("Le texte est un tautogramme")
Else
Console.Write("Le texte n'est pas un tautogramme")
End If
Console.ReadLine()
End Sub
End Module

16.
Module Module1
Dim i, j, n, s, c, m As Integer
Sub Main()
Console.Write("Entrez la taille du Tableau 1 : ")
n = Console.ReadLine
Dim t1(n) As Integer
For i = 0 To n - 1
Console.Write("t(" & i + 1 & ")=")
t1(i) = Console.ReadLine
Next
Console.Write("Entrez la taille du Tableau 2 : ")
m = Console.ReadLine
Dim t2(m) As Integer
For i = 0 To m - 1
Console.Write("t(" & i + 1 & ")=")
t2(i) = Console.ReadLine
Next
Dim t(c) As Integer
s = 0
c = 0
For i = 0 To n - 1
For j = 0 To m - 1
If t1(s) = t2(j) Then
t(c) = t1(s)
c = c + 1
ReDim Preserve t(c)
End If
Next
s = s + 1
Next
For i = 0 To c - 1
Console.Write(t(i) & " ")
Next
Console.ReadLine()
End Sub
End Module

17.
Module Module1
Dim a, b, i, j, x, y, z, PGDC As Integer
Sub Main()
Console.Write("Entrez a : ")
a = Console.ReadLine
Console.Write("Entrez b : ")
b = Console.ReadLine
x = 0
y = 0
z = 0
Dim t1(x) As Integer
For i = 1 To a
If a Mod i = 0 Then
ReDim Preserve t1(x)
t1(x) = i
x = x + 1
End If
Next
Dim t2(y) As Integer
For i = 1 To b
If b Mod i = 0 Then
ReDim Preserve t2(y)
t2(y) = i
y = y + 1
End If
Next
Dim t(z) As Integer
For i = 0 To x - 2
For j = 0 To y - 2
If t1(i) = t2(j) Then
ReDim Preserve t(z)
t(z) = t1(i)
z = z + 1
End If
Next
Next
PGDC = t(0)
For i = 0 To t.Length - 1
If t(i) > PGDC Then
PGDC = t(i)
End If
Next
Console.Write("Le plus grand diviseur commun est :" & PGDC)
Console.ReadLine()
End Sub
End Module

0 commentaires:

Enregistrer un commentaire